Module: Ardm::ActiveRecord::Property
- Extended by:
- ActiveSupport::Concern
- Included in:
- Base
- Defined in:
- lib/ardm/active_record/property.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#attribute_get(name) ⇒ Object
This not the same as read_attribute in AR.
-
#attribute_set(name, value) ⇒ Object
This not the same as write_attribute in AR.
-
#fields ⇒ Array<Property>
private
Fetches all the names of the attributes that have been loaded, even if they are lazy but have been called.
-
#initialize_ardm_property_defaults ⇒ Object
when exactly does a datamapper default property get set?.
-
#key ⇒ Array(Key)
Retrieve the key(s) for this resource.
-
#properties ⇒ PropertySet
private
Gets this instance’s Model’s properties.
-
#reset_key ⇒ undefined
private
Reset the key to the original value.
Class Method Details
.extended(model) ⇒ Object
18 19 20 |
# File 'lib/ardm/active_record/property.rb', line 18 def self.extended(model) raise "Please include #{self} instead of extend." end |
Instance Method Details
#attribute_get(name) ⇒ Object
This not the same as read_attribute in AR
291 292 293 294 295 296 297 298 299 |
# File 'lib/ardm/active_record/property.rb', line 291 def attribute_get(name) if property = self.class.properties[name] val = read_attribute property.field if new_record? && val.nil? && property.default? write_attribute property.field, property.typecast(property.default_for(self)) end read_attribute property.field end end |
#attribute_set(name, value) ⇒ Object
This not the same as write_attribute in AR
302 303 304 305 306 307 |
# File 'lib/ardm/active_record/property.rb', line 302 def attribute_set(name, value) if property = self.class.properties[name] write_attribute property.field, property.typecast(value) read_attribute property.field end end |
#fields ⇒ Array<Property>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Fetches all the names of the attributes that have been loaded, even if they are lazy but have been called
351 352 353 354 355 |
# File 'lib/ardm/active_record/property.rb', line 351 def fields properties.select do |property| property.loaded?(self) || (new_record? && property.default?) end end |
#initialize_ardm_property_defaults ⇒ Object
when exactly does a datamapper default property get set?
282 283 284 285 286 287 288 |
# File 'lib/ardm/active_record/property.rb', line 282 def initialize_ardm_property_defaults return unless new_record? self.class.properties.each do |property| attribute_get(property.name) # assigns default on fetch end true end |
#key ⇒ Array(Key)
Retrieve the key(s) for this resource.
This always returns the persisted key value, even if the key is changed and not yet persisted. This is done so all relations still work.
319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/ardm/active_record/property.rb', line 319 def key return @_key if defined?(@_key) model_key = self.class.key key = model_key.map do |property| changed_attributes[property.name] || (property.loaded?(self) ? property.get!(self) : nil) end # only memoize a valid key @_key = key if model_key.valid?(key) key end |
#properties ⇒ PropertySet
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Gets this instance’s Model’s properties
340 341 342 |
# File 'lib/ardm/active_record/property.rb', line 340 def properties self.class.properties end |
#reset_key ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reset the key to the original value
362 363 364 365 366 |
# File 'lib/ardm/active_record/property.rb', line 362 def reset_key properties.key.zip(key) do |property, value| property.set!(self, value) end end |