Module: Ardm::ActiveRecord::Property::ClassMethods

Defined in:
lib/ardm/active_record/property.rb

Instance Method Summary collapse

Instance Method Details

#columnsObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ardm/active_record/property.rb', line 102

def columns
  @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.name.to_s,
      property.field.to_s,
      nil,#property.dump(property.default),
      sql_type,
      property.allow_nil?
    )

    column.primary = property.key?
    column
  end
end

#dump_properties_hash(options) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/ardm/active_record/property.rb', line 133

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) ⇒ Object

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



129
130
131
# File 'lib/ardm/active_record/property.rb', line 129

def expand_hash_conditions_for_aggregates(*args)
  dump_properties_hash(super)
end

#field_naming_convention#call

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

Returns:

  • (#call)

    The naming convention for the given Repository



165
166
167
# File 'lib/ardm/active_record/property.rb', line 165

def field_naming_convention
  @field_naming_convention ||= lambda { |property| property.name.to_s.underscore }
end

#inherited(model) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/ardm/active_record/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 = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ardm/active_record/property.rb', line 90

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

#keyArray

Gets the list of key fields for this Model

Returns:

  • (Array)

    The list of key fields for this Model



150
151
152
# File 'lib/ardm/active_record/property.rb', line 150

def key
  properties.key
end

#key_conditions(key) ⇒ Object

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.



183
184
185
# File 'lib/ardm/active_record/property.rb', line 183

def key_conditions(key)
  Hash[ self.key.zip(key.nil? ? [] : key) ]
end

#propertiesPropertySet

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

Returns:

  • (PropertySet)

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



86
87
88
# File 'lib/ardm/active_record/property.rb', line 86

def properties
  @properties ||= PropertySet.new
end

#properties_with_subclassesObject

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.



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ardm/active_record/property.rb', line 170

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 = {}) ⇒ Property

Defines a Property on the Resource

Parameters:

  • name (Symbol)

    the name for which to call this property

  • type (Class)

    the ruby type to define this property as

  • options (Hash(Symbol => String)) (defaults to: {})

    a hash of available options

Returns:

See Also:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ardm/active_record/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)

  unless klass
    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

#serialObject



155
156
157
# File 'lib/ardm/active_record/property.rb', line 155

def serial
  key.detect { |property| property.serial? }
end