Method: MongoDoc::Document::ClassMethods#bson_create

Defined in:
lib/mongo_doc/document.rb

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mongo_doc/document.rb', line 131

def bson_create(bson_hash, options = {})
  allocate.tap do |obj|
    bson_hash.each do |name, value|
      if MongoDoc::Configuration.dynamic_attributes
        if _attributes.include?(name.to_sym) || name == '_id'
          obj.send("#{name}=", MongoDoc::BSON.decode(value, options))
        else
          obj.send(:dynamic_attributes)[name] = MongoDoc::BSON.decode(value, options)
          obj.singleton_class.module_eval ("def #{name}; dynamic_attributes[#{name.inspect}]; end")
        end
      else
        obj.send("#{name}=", MongoDoc::BSON.decode(value, options))
      end
    end
  end
end