Module: Ardm::Ar::Property::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#_ardm_load_columnsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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) ⇒ Object



160
161
162
163
164
165
# 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

#columnsObject



115
116
117
# File 'lib/ardm/ar/property.rb', line 115

def columns
  @columns ||= _ardm_load_columns
end

#dump_properties_hash(options) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# 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) ⇒ 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



145
146
147
# File 'lib/ardm/ar/property.rb', line 145

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



188
189
190
# 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) ⇒ Object



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



103
104
105
106
107
108
109
110
111
112
113
# 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

#keyArray

Gets the list of key fields for this Model

Returns:

  • (Array)

    The list of key fields for this Model



173
174
175
# File 'lib/ardm/ar/property.rb', line 173

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.



206
207
208
# File 'lib/ardm/ar/property.rb', line 206

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/ar/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.



193
194
195
196
197
198
199
200
201
202
203
# 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 = {}) ⇒ 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/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

#serialObject



178
179
180
# File 'lib/ardm/ar/property.rb', line 178

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

#timestamps(at = :at) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# 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