module Believer::Columns

Defines methods for dealing with model attributes.

Public Instance Methods

[](attr_name) click to toggle source

Returns the value of the attribute identified by attr_name.

# File lib/believer/columns.rb, line 11
def [](attr_name)
  read_attribute(attr_name)
end
[]=(attr_name, value) click to toggle source

Updates the attribute identified by attr_name with the specified value.

# File lib/believer/columns.rb, line 16
def []=(attr_name, value)
  write_attribute(attr_name, value)
end
attribute_names() click to toggle source

Returns an array of names for the attributes available on this object.

# File lib/believer/columns.rb, line 138
def attribute_names
  @attributes.keys
end
attributes() click to toggle source

Returns a hash of all the attributes with their names as keys and the values of the attributes as values.

# File lib/believer/columns.rb, line 143
def attributes
  attrs = {}
  attribute_names.each { |name| attrs[name] = read_attribute(name) }
  attrs
end
attributes=(attrs) click to toggle source
# File lib/believer/columns.rb, line 149
def attributes=(attrs)
  attrs.each do |name, value|
    setter_method = "#{name}=".to_sym
    self.send(setter_method, value) if respond_to?(setter_method)
  end if attrs.present?
end
attributes_dup() click to toggle source

Returns a hash of all the attributes with their names as keys and the values of the attributes as values.

# File lib/believer/columns.rb, line 157
def attributes_dup
  @attributes.dup
end
equal_key_values?(obj) click to toggle source
# File lib/believer/columns.rb, line 93
def equal_key_values?(obj)
  self.class.primary_key_columns.all? do |key_col|
    read_attribute(key_col) == obj.read_attribute(key_col)
  end
end
has_attribute?(attr_name) click to toggle source

Returns true if the given attribute is in the attributes hash

# File lib/believer/columns.rb, line 133
def has_attribute?(attr_name)
  @attributes.has_key?(attr_name.to_s)
end
key_values() click to toggle source
# File lib/believer/columns.rb, line 99
def key_values
  k = {}
  self.class.primary_key_columns.each do |key_col|
    k[key_col] = read_attribute(key_col)
  end
  k
end
merge_attributes(attrs) click to toggle source

protected

# File lib/believer/columns.rb, line 162
def merge_attributes(attrs)
  @attributes ||= {}
  attrs.each_pair do |k, v|
    @attributes[k.to_sym] = v
  end
end
read_attribute(attr_name) click to toggle source
# File lib/believer/columns.rb, line 107
def read_attribute(attr_name)
  col = self.class.columns[attr_name]
  if !@attributes.has_key?(attr_name) && col && col.has_default_value?
    write_attribute(attr_name, col.default_value)
  end
  @attributes[attr_name]
end
write_attribute(attr_name, value) click to toggle source
# File lib/believer/columns.rb, line 115
def write_attribute(attr_name, value)
  v = value
  # Convert the value to the actual type
  col = self.class.columns[attr_name]
  unless col.nil?
    cur_val = @attributes[attr_name]
    if cur_val && cur_val.respond_to?(:adopt_value)
      cur_val.adopt_value(value)
      v = cur_val
    else
      v = col.convert_to_type(v)
    end
    v = col.default_value if v.nil? && col.has_default_value?
  end
  @attributes[attr_name] = v
end