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
-
#attributes_dup ⇒ Object
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
- #equal_key_values?(obj) ⇒ Boolean
-
#has_attribute?(attr_name) ⇒ Boolean
Returns true if the given attribute is in the attributes hash.
- #key_values ⇒ Object
-
#merge_attributes(attrs) ⇒ Object
protected.
- #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.
138 139 140 |
# File 'lib/believer/columns.rb', line 138 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.
143 144 145 146 147 |
# File 'lib/believer/columns.rb', line 143 def attributes attrs = {} attribute_names.each { |name| attrs[name] = read_attribute(name) } attrs end |
#attributes=(attrs) ⇒ Object
149 150 151 152 153 154 |
# 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 ⇒ Object
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
157 158 159 |
# File 'lib/believer/columns.rb', line 157 def attributes_dup @attributes.dup end |
#equal_key_values?(obj) ⇒ Boolean
93 94 95 96 97 |
# 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) ⇒ Boolean
Returns true if the given attribute is in the attributes hash
133 134 135 |
# File 'lib/believer/columns.rb', line 133 def has_attribute?(attr_name) @attributes.has_key?(attr_name.to_s) end |
#key_values ⇒ Object
99 100 101 102 103 104 105 |
# 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) ⇒ Object
protected
162 163 164 165 166 167 |
# 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) ⇒ Object
107 108 109 110 111 112 113 |
# 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) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# 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 |