Module: Believer::Columns
Overview
Defines methods for dealing with model attributes.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#attribute_names ⇒ Object
Returns an array of names for the attributes available on this object.
-
#attributes ⇒ Object
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
- #attributes=(attrs) ⇒ Object
- #equal_key_values?(obj) ⇒ Boolean
-
#has_attribute?(attr_name) ⇒ Boolean
Returns true if the given attribute is in the attributes hash.
- #key_values ⇒ Object
- #read_attribute(attr_name) ⇒ Object
- #write_attribute(attr_name, value) ⇒ Object
Instance Method Details
#attribute_names ⇒ Object
Returns an array of names for the attributes available on this object.
127 128 129 |
# File 'lib/believer/columns.rb', line 127 def attribute_names @attributes.keys end |
#attributes ⇒ Object
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
132 133 134 135 136 |
# File 'lib/believer/columns.rb', line 132 def attributes attrs = {} attribute_names.each { |name| attrs[name] = read_attribute(name) } attrs end |
#attributes=(attrs) ⇒ Object
138 139 140 141 142 143 |
# File 'lib/believer/columns.rb', line 138 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 |
#equal_key_values?(obj) ⇒ Boolean
83 84 85 86 87 |
# File 'lib/believer/columns.rb', line 83 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) ⇒ Boolean
Returns true if the given attribute is in the attributes hash
122 123 124 |
# File 'lib/believer/columns.rb', line 122 def has_attribute?(attr_name) @attributes.has_key?(attr_name.to_s) end |
#key_values ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/believer/columns.rb', line 89 def key_values k = {} self.class.primary_key_columns.each do |key_col| k[key_col] = read_attribute(key_col) end k end |
#read_attribute(attr_name) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/believer/columns.rb', line 97 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) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/believer/columns.rb', line 105 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 end @attributes[attr_name] = v end |