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

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



282
283
284
285
286
287
288
289
# File 'lib/ardm/active_record/property.rb', line 282

def attribute_get(name)
  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

#attribute_set(name, value) ⇒ Object

This not the same as write_attribute in AR



292
293
294
295
296
# File 'lib/ardm/active_record/property.rb', line 292

def attribute_set(name, value)
  property = self.class.properties[name]
  write_attribute property.field, property.typecast(value)
  read_attribute property.field
end

#fieldsArray<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

Returns:

  • (Array<Property>)

    names of attributes that have been loaded



338
339
340
341
342
# File 'lib/ardm/active_record/property.rb', line 338

def fields
  properties.select do |property|
    property.loaded?(self) || (new_record? && property.default?)
  end
end

#initialize_ardm_property_defaultsObject

when exactly does a datamapper default property get set?



273
274
275
276
277
278
279
# File 'lib/ardm/active_record/property.rb', line 273

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

#keyArray(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.

Returns:

  • (Array(Key))

    the key(s) identifying this resource



308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/ardm/active_record/property.rb', line 308

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)
end

#propertiesPropertySet

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

Returns:

  • (PropertySet)

    List of this Resource’s Model’s properties



327
328
329
# File 'lib/ardm/active_record/property.rb', line 327

def properties
  self.class.properties
end

#reset_keyundefined

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

Returns:

  • (undefined)


349
350
351
352
353
# File 'lib/ardm/active_record/property.rb', line 349

def reset_key
  properties.key.zip(key) do |property, value|
    property.set!(self, value)
  end
end