Sha256: c99a13572c86638d02f80c9bf66e434299ea050c74b34cb8b02d287190963266

Contents?: true

Size: 799 Bytes

Versions: 41

Compression:

Stored size: 799 Bytes

Contents

require 'mongo_doc/ext'

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

41 entries across 41 versions & 2 rubygems

Version Path
mongo_doc-0.6.34 lib/mongo_doc/bson.rb
mongo_doc-0.6.33 lib/mongo_doc/bson.rb
mongo_doc-0.6.32 lib/mongo_doc/bson.rb
mongo_doc-0.6.31 lib/mongo_doc/bson.rb
mongo_doc-0.6.30 lib/mongo_doc/bson.rb
mongo_doc-0.6.29 lib/mongo_doc/bson.rb
mongo_doc-0.6.28 lib/mongo_doc/bson.rb
mongo_doc-0.6.27 lib/mongo_doc/bson.rb
mongo_doc-0.6.26 lib/mongo_doc/bson.rb
mongo_doc-0.6.25 lib/mongo_doc/bson.rb
mongo_doc-0.6.23 lib/mongo_doc/bson.rb
mongo_doc-0.6.22 lib/mongo_doc/bson.rb
mongo_doc-0.6.21 lib/mongo_doc/bson.rb
mongo_doc-0.6.20 lib/mongo_doc/bson.rb
mongo_doc-0.6.19 lib/mongo_doc/bson.rb
mongo_doc-0.6.18 lib/mongo_doc/bson.rb
mongo_doc-0.6.17 lib/mongo_doc/bson.rb
mongo_doc-0.6.16 lib/mongo_doc/bson.rb
mongo_doc-0.6.15 lib/mongo_doc/bson.rb
mongo_doc-0.6.14 lib/mongo_doc/bson.rb