Module: MongoDoc::BSON

Defined in:
lib/mongo_doc/bson.rb

Constant Summary collapse

CLASS_KEY =
"json_class"

Class Method Summary collapse

Class Method Details

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



26
27
28
29
# File 'lib/mongo_doc/bson.rb', line 26

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



19
20
21
22
23
24
# File 'lib/mongo_doc/bson.rb', line 19

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



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mongo_doc/bson.rb', line 7

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