Module: MongoMapper::Document::InstanceMethods

Defined in:
lib/mongo_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#_root_documentObject

Used by embedded docs to find root easily without if/respond_to? stuff. Documents are always root documents.



334
335
336
# File 'lib/mongo_mapper/document.rb', line 334

def _root_document
  self
end

#collectionObject



290
291
292
# File 'lib/mongo_mapper/document.rb', line 290

def collection
  self.class.collection
end

#databaseObject



294
295
296
# File 'lib/mongo_mapper/document.rb', line 294

def database
  self.class.database
end

#deleteObject



313
314
315
316
# File 'lib/mongo_mapper/document.rb', line 313

def delete
  @_destroyed = true
  self.class.delete(id) unless new?
end

#destroyObject



309
310
311
# File 'lib/mongo_mapper/document.rb', line 309

def destroy
  delete
end

#destroyed?Boolean

Returns:



318
319
320
# File 'lib/mongo_mapper/document.rb', line 318

def destroyed?
  @_destroyed == true
end

#reloadObject



322
323
324
325
326
327
328
329
330
# File 'lib/mongo_mapper/document.rb', line 322

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



298
299
300
301
302
# File 'lib/mongo_mapper/document.rb', line 298

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



304
305
306
307
# File 'lib/mongo_mapper/document.rb', line 304

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