Sha256: fa3906fb939cb69d957f6b816932e0d5c168dc6c2cd687c2b5bd18e4d81d9c50

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

require 'mongo_doc/ext/array'
require 'mongo_doc/ext/binary'
require 'mongo_doc/ext/boolean_class'
require 'mongo_doc/ext/date'
require 'mongo_doc/ext/date_time'
require 'mongo_doc/ext/dbref'
require 'mongo_doc/ext/hash'
require 'mongo_doc/ext/nil_class'
require 'mongo_doc/ext/numeric'
require 'mongo_doc/ext/object'
require 'mongo_doc/ext/object_id'
require 'mongo_doc/ext/regexp'
require 'mongo_doc/ext/string'
require 'mongo_doc/ext/symbol'
require 'mongo_doc/ext/time'

module MongoDoc
  module BSON
    CLASS_KEY = "json_class"

    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
    
    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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongo_doc-0.3.1 lib/mongo_doc/bson.rb
mongo_doc-0.3.0 lib/mongo_doc/bson.rb