Module: MongoDoc::Document::ClassMethods

Defined in:
lib/mongo_doc/document.rb

Instance Method Summary collapse

Instance Method Details

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mongo_doc/document.rb', line 142

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

#collectionObject



159
160
161
# File 'lib/mongo_doc/document.rb', line 159

def collection
  @collection ||= MongoDoc::Collection.new(collection_name)
end

#create(attrs = {}) ⇒ Object



163
164
165
166
167
# File 'lib/mongo_doc/document.rb', line 163

def create(attrs = {})
  instance = new(attrs)
  instance.save(true)
  instance
end

#create!(attrs = {}) ⇒ Object



169
170
171
172
173
# File 'lib/mongo_doc/document.rb', line 169

def create!(attrs = {})
  instance = new(attrs)
  instance.save!
  instance
end