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.



267
268
269
# File 'lib/mongo_mapper/document.rb', line 267

def _root_document
  self
end

#deleteObject



242
243
244
245
# File 'lib/mongo_mapper/document.rb', line 242

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

#destroyObject



238
239
240
# File 'lib/mongo_mapper/document.rb', line 238

def destroy
  delete
end

#destroyed?Boolean

Returns:



251
252
253
# File 'lib/mongo_mapper/document.rb', line 251

def destroyed?
  @_destroyed == true
end

#new?Boolean

Returns:



247
248
249
# File 'lib/mongo_mapper/document.rb', line 247

def new?
  @new
end

#reloadObject



255
256
257
258
259
260
261
262
263
# File 'lib/mongo_mapper/document.rb', line 255

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

#save(options = {}) ⇒ Object



227
228
229
230
231
# File 'lib/mongo_mapper/document.rb', line 227

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



233
234
235
236
# File 'lib/mongo_mapper/document.rb', line 233

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