module Ardm::Ar::Property

Public Class Methods

extended(model) click to toggle source
# File lib/ardm/ar/property.rb, line 18
def self.extended(model)
  raise "Please include #{self} instead of extend."
end

Public Instance Methods

assign_attributes(attrs, *a) click to toggle source
Calls superclass method
# 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) click to toggle source

This not the same as read_attribute in AR

# 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) click to toggle source

This not the same as write_attribute in AR

# 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() click to toggle source

Fetches all the names of the attributes that have been loaded, even if they are lazy but have been called

@return [Array<Property>]

names of attributes that have been loaded

@api private

# 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() click to toggle source

when exactly does a datamapper default property get set?

# 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() click to toggle source

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.

@return [Array(Key)]

the key(s) identifying this resource

@api public

# 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() click to toggle source

Gets this instance’s Model’s properties

@return [PropertySet]

List of this Resource's Model's properties

@api private

# File lib/ardm/ar/property.rb, line 365
def properties
  self.class.properties
end
reset_key() click to toggle source

Reset the key to the original value

@return [undefined]

@api private

# File lib/ardm/ar/property.rb, line 387
def reset_key
  properties.key.zip(key) do |property, value|
    property.set!(self, value)
  end
end