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

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

Instance Method Summary collapse

Instance Method Details

#_ardm_load_columnsObject



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

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



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

def assert_valid_attributes(options)
  options.each do |key, value|
    property = properties[key]
    property.assert_valid_value(value)
  end
end

#columnsObject



102
103
104
# File 'lib/ardm/active_record/property.rb', line 102

def columns
  @columns ||= _ardm_load_columns
end

#dump_properties_hash(options) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/ardm/active_record/property.rb', line 136

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



132
133
134
# File 'lib/ardm/active_record/property.rb', line 132

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



175
176
177
# File 'lib/ardm/active_record/property.rb', line 175

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



160
161
162
# File 'lib/ardm/active_record/property.rb', line 160

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.



193
194
195
# File 'lib/ardm/active_record/property.rb', line 193

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.



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ardm/active_record/property.rb', line 180

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)

  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

#serialObject



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

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