Module: MongoMapper::Document::InstanceMethods

Defined in:
lib/mongo_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



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

def collection
  self.class.collection
end

#databaseObject



370
371
372
# File 'lib/mongo_mapper/document.rb', line 370

def database
  self.class.database
end

#deleteObject



396
397
398
# File 'lib/mongo_mapper/document.rb', line 396

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

#destroyObject



392
393
394
# File 'lib/mongo_mapper/document.rb', line 392

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

#new?Boolean

Returns:



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

def new?
  read_attribute('_id').blank? || using_custom_id?
end

#reloadObject



400
401
402
403
404
405
# File 'lib/mongo_mapper/document.rb', line 400

def reload
  doc = self.class.find(_id)
  self.class.associations.each { |name, assoc| send(name).reset if respond_to?(name) }
  self.attributes = doc.attributes
  self
end

#save(options = {}) ⇒ Object



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

def save(options={})
  if options === false
    ActiveSupport::Deprecation.warn "save with true/false is deprecated. You should now use :validate => true/false."
    options = {:validate => false}
  end
  options.reverse_merge!(:validate => true)
  perform_validations = options.delete(:validate)
  !perform_validations || valid? ? create_or_update(options) : false
end

#save!Object



388
389
390
# File 'lib/mongo_mapper/document.rb', line 388

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