Module: Mongoid::Persistence::InstanceMethods
- Defined in:
- lib/mongoid/persistence.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#_remove ⇒ Object
(also: #delete)
Remove the
Document
from the datbase. -
#destroy ⇒ Object
Remove the
Document
from the datbase with callbacks. -
#insert(validate = true) ⇒ Object
Insert a new
Document
into the database. -
#save! ⇒ Object
Save the document - will perform an insert if the document is new, and update if not.
-
#update(validate = true) ⇒ Object
Update the
Document
in the datbase. -
#update_attributes(attributes = {}) ⇒ Object
Update the
Document
attributes in the datbase. -
#update_attributes!(attributes = {}) ⇒ Object
Update the
Document
attributes in the datbase. -
#upsert(validate = true) ⇒ Object
(also: #save)
Upsert the document - will perform an insert if the document is new, and update if not.
Instance Method Details
#_remove ⇒ Object Also known as: delete
Remove the Document
from the datbase.
Example:
document._remove
TODO: Will get rid of other #remove once observable pattern killed.
55 56 57 |
# File 'lib/mongoid/persistence.rb', line 55 def _remove Remove.new(self).persist end |
#destroy ⇒ Object
Remove the Document
from the datbase with callbacks.
Example:
document.destroy
TODO: Will get rid of other #destroy once new persistence complete.
30 31 32 33 34 35 36 |
# File 'lib/mongoid/persistence.rb', line 30 def destroy run_callbacks(:before_destroy) if _remove self.destroyed = true run_callbacks(:after_destroy) end; true end |
#insert(validate = true) ⇒ Object
Insert a new Document
into the database. Will return the document itself whether or not the save was successful.
Example:
document.insert
44 45 46 |
# File 'lib/mongoid/persistence.rb', line 44 def insert(validate = true) Insert.new(self, validate).persist end |
#save! ⇒ Object
Save the document - will perform an insert if the document is new, and update if not. If a validation error occurs a Mongoid::Errors::Validations error will get raised.
Example:
document.save!
Returns:
true
if validation passed, will raise error otherwise.
72 73 74 |
# File 'lib/mongoid/persistence.rb', line 72 def save! self.class.fail_validate!(self) unless upsert; true end |
#update(validate = true) ⇒ Object
Update the Document
in the datbase.
Example:
document.update
81 82 83 |
# File 'lib/mongoid/persistence.rb', line 81 def update(validate = true) Update.new(self, validate).persist end |
#update_attributes(attributes = {}) ⇒ Object
Update the Document
attributes in the datbase.
Example:
document.update_attributes(:title => "Sir")
Returns:
true
if validation passed, false
if not.
94 95 96 |
# File 'lib/mongoid/persistence.rb', line 94 def update_attributes(attributes = {}) write_attributes(attributes); update end |
#update_attributes!(attributes = {}) ⇒ Object
Update the Document
attributes in the datbase.
Example:
document.update_attributes(:title => "Sir")
Returns:
true
if validation passed, raises an error if not
107 108 109 110 111 112 |
# File 'lib/mongoid/persistence.rb', line 107 def update_attributes!(attributes = {}) write_attributes(attributes) result = update self.class.fail_validate!(self) unless result result end |
#upsert(validate = true) ⇒ Object Also known as: save
Upsert the document - will perform an insert if the document is new, and update if not.
Example:
document.upsert
Returns:
A Boolean
for updates.
124 125 126 127 128 129 130 131 |
# File 'lib/mongoid/persistence.rb', line 124 def upsert(validate = true) validate = parse_validate(validate) if new_record? insert(validate).errors.any? ? false : true else update(validate) end end |