Module: MongoMapper::Document::InstanceMethods

Defined in:
lib/mongo_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



332
333
334
# File 'lib/mongo_mapper/document.rb', line 332

def collection
  self.class.collection
end

#databaseObject



336
337
338
# File 'lib/mongo_mapper/document.rb', line 336

def database
  self.class.database
end

#deleteObject



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

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

#destroyObject



360
361
362
# File 'lib/mongo_mapper/document.rb', line 360

def destroy
  delete
end

#reloadObject



368
369
370
371
372
373
374
375
376
# File 'lib/mongo_mapper/document.rb', line 368

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



340
341
342
343
344
# File 'lib/mongo_mapper/document.rb', line 340

def save(options={})
  options.reverse_merge!(:validate => true)
  perform_validations = options.delete(:validate)
  !perform_validations || valid? ? create_or_update(options) : false
end

#save!Object



346
347
348
# File 'lib/mongo_mapper/document.rb', line 346

def save!
  save || raise(DocumentNotValid.new(self))
end

#update_attributes(attrs = {}) ⇒ Object



350
351
352
353
# File 'lib/mongo_mapper/document.rb', line 350

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

#update_attributes!(attrs = {}) ⇒ Object



355
356
357
358
# File 'lib/mongo_mapper/document.rb', line 355

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