Module: Ardm::Ar::Property
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #assign_attributes(attrs, *a) ⇒ Object
-
#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/ar/property.rb', line 18 def self.extended(model) raise "Please include #{self} instead of extend." end |
Instance Method Details
#assign_attributes(attrs, *a) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/ardm/ar/property.rb', line 322 def assign_attributes(attrs, *a) new_attrs = attrs.inject({}) do |memo,(name,value)| if property = self.class.properties[name] memo[property.field] = property.typecast(value) else memo[name] = value end memo end super new_attrs, *a end |
#attribute_get(name) ⇒ Object
This not the same as read_attribute in AR
304 305 306 307 308 309 310 311 312 |
# File 'lib/ardm/ar/property.rb', line 304 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
315 316 317 318 319 320 |
# File 'lib/ardm/ar/property.rb', line 315 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
376 377 378 379 380 |
# File 'lib/ardm/ar/property.rb', line 376 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?
295 296 297 298 299 300 301 |
# File 'lib/ardm/ar/property.rb', line 295 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.
344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/ardm/ar/property.rb', line 344 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
365 366 367 |
# File 'lib/ardm/ar/property.rb', line 365 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
387 388 389 390 391 |
# File 'lib/ardm/ar/property.rb', line 387 def reset_key properties.key.zip(key) do |property, value| property.set!(self, value) end end |