Module: Ardm::Ar::Property

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/ardm/ar/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/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

#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



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_defaultsObject

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

#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



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

#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



365
366
367
# File 'lib/ardm/ar/property.rb', line 365

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)


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