Module: MongoMapper::Document::InstanceMethods

Defined in:
lib/mongo_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



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

def collection
  self.class.collection
end

#databaseObject



379
380
381
# File 'lib/mongo_mapper/document.rb', line 379

def database
  self.class.database
end

#deleteObject



409
410
411
# File 'lib/mongo_mapper/document.rb', line 409

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

#destroyObject



405
406
407
# File 'lib/mongo_mapper/document.rb', line 405

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

#lock_version_fieldObject



383
384
385
# File 'lib/mongo_mapper/document.rb', line 383

def lock_version_field
  self.class.lock_version_field
end

#new?Boolean

Returns:



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

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

#reloadObject



413
414
415
416
417
418
# File 'lib/mongo_mapper/document.rb', line 413

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



391
392
393
394
395
396
397
398
399
# File 'lib/mongo_mapper/document.rb', line 391

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



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

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