Module: MongoModel::Attributes::Mongo

Included in:
Store
Defined in:
lib/mongomodel/attributes/mongo.rb

Instance Method Summary collapse

Instance Method Details

#from_mongo!(hash) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongomodel/attributes/mongo.rb', line 23

def from_mongo!(hash)
  hash.each do |k, v|
    property = properties_as[k.to_s]

    if property
      child = store(property.name, property.from_mongo(v))
    else
      child = store(k.to_sym, v)
    end

    child.parent_document = instance if child.respond_to?(:parent_document=)
  end
end

#load!(hash) ⇒ Object


19
20
21
# File 'lib/mongomodel/attributes/mongo.rb', line 19

def load!(hash)
  from_mongo!(hash)
end

#to_mongoObject


4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mongomodel/attributes/mongo.rb', line 4

def to_mongo
  inject({}) do |result, (k, v)|
    property = properties[k]

    if property
      result[property.as] = property.to_mongo(v)
    else
      converter = Types.converter_for(v.class)
      result[k.to_s] = converter.to_mongo(v)
    end

    result
  end
end