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.13 lib/mongo_doc/bson.rb
mongo_doc-0.6.12 lib/mongo_doc/bson.rb
mongo_doc-0.6.11 lib/mongo_doc/bson.rb
mongo_doc-0.6.10 lib/mongo_doc/bson.rb
mongo_doc-0.6.9 lib/mongo_doc/bson.rb
mongo_doc-0.6.8 lib/mongo_doc/bson.rb
mongo_doc-0.6.7 lib/mongo_doc/bson.rb
mongo_doc-0.6.6 lib/mongo_doc/bson.rb
mongo_doc-0.6.5 lib/mongo_doc/bson.rb
mongo_doc-0.6.4 lib/mongo_doc/bson.rb
mongo_doc_rails2-0.6.2 lib/mongo_doc/bson.rb
mongo_doc-0.6.3 lib/mongo_doc/bson.rb
mongo_doc-0.6.2 lib/mongo_doc/bson.rb
mongo_doc-0.6.1 lib/mongo_doc/bson.rb
mongo_doc-0.6.0 lib/mongo_doc/bson.rb
mongo_doc_rails2-0.6.1 lib/mongo_doc/bson.rb
mongo_doc-0.5.5 lib/mongo_doc/bson.rb
mongo_doc-0.4.2 lib/mongo_doc/bson.rb
mongo_doc-0.4.1 lib/mongo_doc/bson.rb
mongo_doc-0.4.0 lib/mongo_doc/bson.rb