Module: MongoMapper::Document::InstanceMethods

Defined in:
lib/novelys_mongo_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



349
350
351
# File 'lib/novelys_mongo_mapper/document.rb', line 349

def collection
  self.class.collection
end

#databaseObject



353
354
355
# File 'lib/novelys_mongo_mapper/document.rb', line 353

def database
  self.class.database
end

#deleteObject



382
383
384
# File 'lib/novelys_mongo_mapper/document.rb', line 382

def delete
  self.class.delete(id) unless new?
end

#destroyObject



378
379
380
# File 'lib/novelys_mongo_mapper/document.rb', line 378

def destroy
  delete
end

#reloadObject



386
387
388
389
390
391
392
393
394
# File 'lib/novelys_mongo_mapper/document.rb', line 386

def reload
  if attrs = collection.find_one({:_id => _id})
    self.class.associations.each { |name, assoc| send(name).reset if respond_to?(name) }
    self.attributes = attrs
    self
  else
    raise DocumentNotFound, "Document match #{_id.inspect} does not exist in #{collection.name} collection"
  end
end

#save(options = {}) ⇒ Object



357
358
359
360
361
# File 'lib/novelys_mongo_mapper/document.rb', line 357

def save(options={})
  options.assert_valid_keys(:validate, :safe)
  options.reverse_merge!(:validate => true)
  !options[:validate] || valid? ? create_or_update(options) : false
end

#save!(options = {}) ⇒ Object



363
364
365
366
# File 'lib/novelys_mongo_mapper/document.rb', line 363

def save!(options={})
  options.assert_valid_keys(:safe)
  save(options) || raise(DocumentNotValid.new(self))
end

#update_attributes(attrs = {}) ⇒ Object



368
369
370
371
# File 'lib/novelys_mongo_mapper/document.rb', line 368

def update_attributes(attrs={})
  self.attributes = attrs
  save
end

#update_attributes!(attrs = {}) ⇒ Object



373
374
375
376
# File 'lib/novelys_mongo_mapper/document.rb', line 373

def update_attributes!(attrs={})
  self.attributes = attrs
  save!
end