Module: MongoMapper::Document::InstanceMethods

Defined in:
lib/mongo_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



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

def collection
  self.class.collection
end

#databaseObject



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

def database
  self.class.database
end

#deleteObject



376
377
378
# File 'lib/mongo_mapper/document.rb', line 376

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

#destroyObject



372
373
374
# File 'lib/mongo_mapper/document.rb', line 372

def destroy
  delete
end

#reloadObject



380
381
382
383
384
385
386
387
388
# File 'lib/mongo_mapper/document.rb', line 380

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



361
362
363
364
365
# File 'lib/mongo_mapper/document.rb', line 361

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



367
368
369
370
# File 'lib/mongo_mapper/document.rb', line 367

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