Module: CouchRest::Model::Persistence
- Extended by:
- ActiveSupport::Concern
- Included in:
- Base
- Defined in:
- lib/couchrest/model/persistence.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#create(options = {}) ⇒ Object
Create the document.
-
#create! ⇒ Object
Creates the document in the db.
-
#destroy ⇒ Object
Deletes the document from the database.
-
#save(options = {}) ⇒ Object
Trigger the callbacks (before, after, around) and save the document.
-
#save! ⇒ Object
Saves the document to the db using save.
-
#update(options = {}) ⇒ Object
Trigger the callbacks (before, after, around) only if the document isn’t new.
-
#update_attributes(hash) ⇒ Object
Update the document’s attributes and save.
Instance Method Details
#create(options = {}) ⇒ Object
Create the document. Validation is enabled by default and will return false if the document is not valid. If all goes well, the document will be returned.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/couchrest/model/persistence.rb', line 9 def create( = {}) return false unless perform_validations() _run_create_callbacks do _run_save_callbacks do set_unique_id if new? && self.respond_to?(:set_unique_id) result = database.save_doc(self) (result["ok"] == true) ? self : false end end end |
#create! ⇒ Object
Creates the document in the db. Raises an exception if the document is not created properly.
22 23 24 |
# File 'lib/couchrest/model/persistence.rb', line 22 def create! self.class.fail_validate!(self) unless self.create end |
#destroy ⇒ Object
Deletes the document from the database. Runs the :destroy callbacks. Removes the _id
and _rev
fields, preparing the document to be saved to a new _id
if required.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/couchrest/model/persistence.rb', line 54 def destroy _run_destroy_callbacks do result = database.delete_doc(self) if result['ok'] self.delete('_rev') self.delete('_id') end result['ok'] end end |
#save(options = {}) ⇒ Object
Trigger the callbacks (before, after, around) and save the document
40 41 42 |
# File 'lib/couchrest/model/persistence.rb', line 40 def save( = {}) self.new? ? create() : update() end |
#save! ⇒ Object
Saves the document to the db using save. Raises an exception if the document is not saved properly.
46 47 48 49 |
# File 'lib/couchrest/model/persistence.rb', line 46 def save! self.class.fail_validate!(self) unless self.save true end |
#update(options = {}) ⇒ Object
Trigger the callbacks (before, after, around) only if the document isn’t new
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/couchrest/model/persistence.rb', line 28 def update( = {}) raise "Calling #{self.class.name}#update on document that has not been created!" if self.new? return false unless perform_validations() _run_update_callbacks do _run_save_callbacks do result = database.save_doc(self) result["ok"] == true end end end |
#update_attributes(hash) ⇒ Object
Update the document’s attributes and save. For example:
doc.update_attributes :name => "Fred"
Is the equivilent of doing the following:
doc.attributes = { :name => "Fred" }
doc.save
74 75 76 77 |
# File 'lib/couchrest/model/persistence.rb', line 74 def update_attributes(hash) update_attributes_without_saving hash save end |