module Riveter::Attributes

Attributes

options[R]

Public Class Methods

_attributes() click to toggle source
# File lib/riveter/attributes.rb, line 18
def self._attributes
  self._attributes_container[self]
end
attributes() click to toggle source
# File lib/riveter/attributes.rb, line 22
def self.attributes
  self._attributes.keys
end
new(params=nil, options={}) click to toggle source
# File lib/riveter/attributes.rb, line 330
def initialize(params=nil, options={})
  # assign default values
  self.class._attributes.each do |name, attribute_info|
    next unless attribute_info.default?
    send("#{name}=", attribute_info.evaluate_default(self))
  end

  @options = options.freeze

  # filter and clean params before applying
  apply_params(
    clean_params(
      filter_params(
        sanitize_for_mass_assignment(params)
      )
    )
  ) unless params.nil?
end

Public Instance Methods

attributes(options={}) click to toggle source
# File lib/riveter/attributes.rb, line 349
def attributes(options={})
  self.class._attributes.inject({}) do |list, (key, attribute_info)|
    list[key] = self.send(attribute_info.name)
    list
  end
end
Also aliased as: to_params
column_for_attribute(column) click to toggle source

forms use this for getting column meta data

# File lib/riveter/attributes.rb, line 368
def column_for_attribute(column)
  attribute_info = self.class._attributes[column]
  OpenStruct.new(
    :name => attribute_info.name,
    :type => attribute_info.type,
    :null => !attribute_info.required?,
    :default => attribute_info.default
  )
end
has_attribute?(column) click to toggle source
# File lib/riveter/attributes.rb, line 363
def has_attribute?(column)
  self.class._attributes.key?(column)
end
persisted?() click to toggle source

forms use this to determine the HTTP verb

# File lib/riveter/attributes.rb, line 359
def persisted?
  false
end
to_params(options={})
Alias for: attributes

Protected Instance Methods

apply_params(params) click to toggle source

given sanitized params, assign values to instance

# File lib/riveter/attributes.rb, line 396
def apply_params(params)
  params.each do |attribute, value|
    assign_attribute(attribute, value)
  end
end
clean_params(params) click to toggle source

remove blank/nil attributes

# File lib/riveter/attributes.rb, line 389
def clean_params(params)
  params.reject { |key, value|
    reject_value?(value)
  }
end
filter_params(params) click to toggle source

only include defined attributes

# File lib/riveter/attributes.rb, line 381
def filter_params(params)
  attributes = self.class._attributes
  params.keep_if {|name, value|
    attributes.key?(name)
  }
end

Private Instance Methods

assign_attribute(attribute, value) click to toggle source
# File lib/riveter/attributes.rb, line 417
def assign_attribute(attribute, value)
  public_send("#{attribute}=", value)

rescue
  if respond_to?("#{attribute}=")
    raise
  else
    raise UnknownAttributeError.new(self, attribute)
  end
end
reject_value?(value) click to toggle source
# File lib/riveter/attributes.rb, line 404
def reject_value?(value)
  case value
  when Array
    value.reject! {|v| reject_value?(v) }
    false
  when Hash
    value.reject! {|k, v| reject_value?(v) }
    false
  else
    value.blank? unless value.is_a?(FalseClass)
  end
end