module Ardm::Ar::Property::ClassMethods

Public Instance Methods

_ardm_load_columns() click to toggle source
# File lib/ardm/ar/property.rb, line 119
def _ardm_load_columns
  properties.map do |property|
    sql_type = connection.type_to_sql(
      property.dump_as.name.to_sym,
      property.options[:limit],
      property.options[:precision],
      property.options[:scale]
    )

    column = ::ActiveRecord::ConnectionAdapters::Column.new(
      property.field.to_s, #property.name.to_s,
      nil,#property.dump(property.default),
      sql_type,
      property.allow_nil?
    )

    column.primary = property.key?
    column
  end
end
assert_valid_attributes(options) click to toggle source
# File lib/ardm/ar/property.rb, line 160
def assert_valid_attributes(options)
  options.each do |key, value|
    property = properties[key]
    property.assert_valid_value(value)
  end
end
columns() click to toggle source
# File lib/ardm/ar/property.rb, line 115
def columns
  @columns ||= _ardm_load_columns
end
dump_properties_hash(options) click to toggle source
# File lib/ardm/ar/property.rb, line 149
def dump_properties_hash(options)
  options.inject({}) do |new_attrs, (key, value)|
    if property = properties[key]
      new_attrs[property.field] = property.dump(value)
    else
      new_attrs[key] = value
    end
    new_attrs
  end
end
expand_hash_conditions_for_aggregates(*args) click to toggle source

Hook into the query system when we would be finding composed_of fields in active record. This lets us mangle the query as needed.

Every DM property needs to be dumped when it’s being sent to a query. This also gives us a chance to handle aliased fields

Calls superclass method
# File lib/ardm/ar/property.rb, line 145
def expand_hash_conditions_for_aggregates(*args)
  dump_properties_hash(super)
end
field_naming_convention() click to toggle source

Gets the field naming conventions for this resource in the given Repository

@return [#call]

The naming convention for the given Repository

@api public

# File lib/ardm/ar/property.rb, line 188
def field_naming_convention
  @field_naming_convention ||= lambda { |property| property.name.to_s.underscore }
end
inherited(model) click to toggle source
Calls superclass method
# File lib/ardm/ar/property.rb, line 24
def inherited(model)
  model.instance_variable_set(:@properties, Ardm::PropertySet.new)
  model.instance_variable_set(:@field_naming_convention, @field_naming_convention)

  model_properties = model.properties
  @properties.each { |property| model_properties << property }

  super
end
initialize_attributes(attributes, options = {}) click to toggle source
Calls superclass method
# File lib/ardm/ar/property.rb, line 103
def initialize_attributes(attributes, options = {})
  super(attributes, options)

  properties.each do |property|
    if attributes.key?(property.name)
      attributes[property.field] = attributes[property.name]
    end
  end

  attributes
end
key() click to toggle source

Gets the list of key fields for this Model

@return [Array]

The list of key fields for this Model

@api public

# File lib/ardm/ar/property.rb, line 173
def key
  properties.key
end
key_conditions(key) click to toggle source

@api private

# File lib/ardm/ar/property.rb, line 206
def key_conditions(key)
  Hash[ self.key.zip(key.nil? ? [] : key) ]
end
properties() click to toggle source

Gets a list of all properties that have been defined on this Model

@return [PropertySet]

A list of Properties defined on this Model in the given Repository

@api public

# File lib/ardm/ar/property.rb, line 86
def properties
  @properties ||= PropertySet.new
end
properties_with_subclasses() click to toggle source

@api private

# File lib/ardm/ar/property.rb, line 193
def properties_with_subclasses
  props = properties.dup

  descendants.each do |model|
    model.properties.each do |property|
      props << property
    end
  end

  props
end
property(name, type, options = {}) click to toggle source

Defines a Property on the Resource

@param [Symbol] name

the name for which to call this property

@param [Class] type

the ruby type to define this property as

@param [Hash(Symbol => String)] options

a hash of available options

@return [Property]

the created Property

@see Property

@api public

# File lib/ardm/ar/property.rb, line 49
def property(name, type, options = {})
  # if the type can be found within Property then
  # use that class rather than the primitive
  klass = Ardm::Property.determine_class(type)

  if !klass || klass == NilClass
    raise ArgumentError, "+type+ was #{type.inspect}, which is not a supported type"
  end

  property = klass.new(self, name, options)

  self.properties << property

  # add the property to the child classes only if the property was
  # added after the child classes' properties have been copied from
  # the parent
  descendants.each do |descendant|
    descendant.properties << property
  end

  serialize(property.field, property)

  set_primary_key_for(property)
  create_reader_for(property)
  create_writer_for(property)
  add_validations_for(property)

  # FIXME: explicit return needed for YARD to parse this properly
  return property
end
serial() click to toggle source

@api public

# File lib/ardm/ar/property.rb, line 178
def serial
  key.detect { |property| property.serial? }
end
timestamps(at=:at) click to toggle source
# File lib/ardm/ar/property.rb, line 90
def timestamps(at=:at)
  case at
  when :at
    property :created_at, DateTime
    property :updated_at, DateTime
  when :on
    property :created_on, Date
    property :updated_on, Date
  else
    raise ArgumentError, "Unknown argument: timestamps(#{at.inspect})"
  end
end

Private Instance Methods

add_validations_for(property) click to toggle source
# File lib/ardm/ar/property.rb, line 274
def add_validations_for(property)
  return if property.key? || property.serial?
  rules = Ardm::Property::Validation.rules_for_property(property)
  rules.each do |options|
    validates(property.name, options)
  end
end
create_reader_for(property) click to toggle source

defines the reader method for the property

@api private

# File lib/ardm/ar/property.rb, line 235
        def create_reader_for(property)
          return if property.key? || property.serial? # let AR do it
          name                   = property.name.to_s
          reader_visibility      = property.reader_visibility
          property_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1
            #{reader_visibility}
            def #{name}
              attribute_get(#{name.inspect})
            end
          RUBY

          if property.kind_of?(Ardm::Property::Boolean)
            boolean_reader_name = "#{name}?"
            property_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1
              #{reader_visibility}
              def #{boolean_reader_name}
                #{name}
              end
            RUBY
          end
        end
create_writer_for(property) click to toggle source

defines the setter for the property

@api private

# File lib/ardm/ar/property.rb, line 260
        def create_writer_for(property)
          return if property.key? || property.serial? # let AR do it
          name              = property.name
          writer_visibility = property.writer_visibility

          writer_name = "#{name}="
          property_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1
            #{writer_visibility}
            def #{writer_name}(value)
              attribute_set(:#{name}, value)
            end
          RUBY
        end
property_module() click to toggle source

Defines the anonymous module that is used to add properties. Using a single module here prevents having a very large number of anonymous modules, where each property has their own module. @api private

# File lib/ardm/ar/property.rb, line 216
def property_module
  @property_module ||= begin
    mod = Module.new
    class_eval do
      include mod
    end
    mod
  end
end
set_primary_key_for(property) click to toggle source
# File lib/ardm/ar/property.rb, line 226
def set_primary_key_for(property)
  if property.key? || property.serial?
    self.primary_key ||= property.name
  end
end