Module: Believer::Columns
Overview
Defines methods for dealing with model attributes.
Defined Under Namespace
Modules: ClassMethods Classes: Column
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.
169 170 171 |
# File 'lib/believer/columns.rb', line 169 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.
174 175 176 177 178 |
# File 'lib/believer/columns.rb', line 174 def attributes attrs = {} attribute_names.each { |name| attrs[name] = read_attribute(name) } attrs end |
#attributes=(attrs) ⇒ Object
180 181 182 183 184 185 |
# File 'lib/believer/columns.rb', line 180 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
134 135 136 137 138 |
# File 'lib/believer/columns.rb', line 134 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
164 165 166 |
# File 'lib/believer/columns.rb', line 164 def has_attribute?(attr_name) @attributes.has_key?(attr_name.to_s) end |
#key_values ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/believer/columns.rb', line 140 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
148 149 150 |
# File 'lib/believer/columns.rb', line 148 def read_attribute(attr_name) @attributes[attr_name] end |
#write_attribute(attr_name, value) ⇒ Object
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/believer/columns.rb', line 152 def write_attribute(attr_name, value) v = value # Convert the value to the actual type unless v.nil? && self.class.columns[attr_name] value_type = self.class.columns[attr_name].type convert_method = "convert_to_#{value_type}".to_sym v = self.send(convert_method, v) if respond_to?(convert_method) end @attributes[attr_name] = v end |