Module: MongoDoc::BSON

Defined in:
lib/mongodoc/bson.rb

Constant Summary collapse

CLASS_KEY =
"json_class"

Class Method Summary collapse

Class Method Details

.array_create(bson_array, options = {}) ⇒ Object



40
41
42
43
# File 'lib/mongodoc/bson.rb', line 40

def self.array_create(bson_array, options = {})
  return bson_array if options[:raw_json]
  bson_array.map {|item| decode(item, options)}
end

.bson_create(bson_hash, options = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/mongodoc/bson.rb', line 33

def self.bson_create(bson_hash, options = {})
  return bson_hash if options[:raw_json]
  klass = bson_hash.delete(CLASS_KEY)
  return bson_hash.each_pair {|key, value| bson_hash[key] = decode(value, options)} unless klass
  klass.constantize.bson_create(bson_hash, options)
end

.decode(bson, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongodoc/bson.rb', line 21

def self.decode(bson, options = {})
  return bson if options[:raw_json]
  case bson
  when Hash
    bson_create(bson, options)
  when Array
    array_create(bson, options)
  else
    bson
  end
end