Module: MongoMapper::Extensions::Hash::ClassMethods

Defined in:
lib/mongo_mapper/extensions/hash.rb

Instance Method Summary collapse

Instance Method Details

#from_mongo(value) ⇒ Object



31
32
33
# File 'lib/mongo_mapper/extensions/hash.rb', line 31

def from_mongo(value)
  HashWithIndifferentAccess.new(value || {})
end

#to_mongo(value, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mongo_mapper/extensions/hash.rb', line 10

def to_mongo(value, options = {})
  stringify_bson = options[:stringify_bson]
  recursive      = options[:recursive]

  value = value.clone if recursive
  hash  = value.to_hash
  hash.each do |k,v|
    
    case v
    when Document, EmbeddedDocument, Hash, Array, Set
      v = v.to_mongo(options) if v and recursive
    when BSON::ObjectId, BSON::Binary
      v = v.to_s if stringify_bson
    end

    hash[k] = v
    
  end
  hash
end