Module: MassiveRecord::ORM::Schema::EmbeddedInterface::ClassMethods

Defined in:
lib/massive_record/orm/schema/embedded_interface.rb

Instance Method Summary collapse

Instance Method Details

#add_field(*args) ⇒ Object

If you need to add fields dynamically, use this method. It wraps functionality needed to keep the class in a consistent state. There is also an instance method defined which will inject default value to the object itself after defining the field.



43
44
45
46
47
48
49
50
51
52
# File 'lib/massive_record/orm/schema/embedded_interface.rb', line 43

def add_field(*args)
  ensure_fields_exists

  field = Field.new_with_arguments_from_dsl(*args)
  fields << field

  undefine_attribute_methods if respond_to? :undefine_attribute_methods

  field
end

#field(*args) ⇒ Object

DSL method exposed into class. Makes it possible to do:

class Person < MassiveRecord::ORM::Embedded

field :name
field :age, :integer, :default => 0
field :points, :integer, :column => :number_of_points

end



27
28
29
30
# File 'lib/massive_record/orm/schema/embedded_interface.rb', line 27

def field(*args)
  ensure_fields_exists
  fields << Field.new_with_arguments_from_dsl(*args)
end

#timestampsObject



33
34
35
# File 'lib/massive_record/orm/schema/embedded_interface.rb', line 33

def timestamps
  add_field :created_at, :time
end

#transpose_raw_data_to_record_attributes_and_raw_data(id, raw_data) ⇒ Object

Returns attributes in embedded object from raw data. Raw data’s keys might different from a field name, if :column option has been used.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/massive_record/orm/schema/embedded_interface.rb', line 60

def transpose_raw_data_to_record_attributes_and_raw_data(id, raw_data)
  attributes = {:id => id}
  attributes['updated_at'] = raw_data.created_at

  raw_attributes =  if raw_data.value.is_a? String
                      Base.coder.load(raw_data.value)
                    else
                      raw_data.value
                    end

  raw_data = Hash[raw_attributes.collect do |attr, value|
    [attr, RawData.new(value: value, created_at: raw_data.created_at)]
  end]

  attributes_schema.each do |attr_name, orm_field|
    value = raw_attributes.has_key?(orm_field.column) ? raw_attributes[orm_field.column] : nil
    attributes[attr_name] = value.nil? ? nil : orm_field.decode(raw_attributes[orm_field.column])
  end

  [attributes, raw_data]
end