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!(options = {}) ⇒ Object
Creates the document in the db.
-
#destroy ⇒ Object
Deletes the document from the database.
- #destroyed? ⇒ Boolean
- #persisted? ⇒ Boolean
-
#reload ⇒ Object
Reloads the attributes of this object 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 19 20 |
# 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) ret = (result["ok"] == true) ? self : false @changed_attributes.clear if ret && @changed_attributes ret end end end |
#create!(options = {}) ⇒ Object
Creates the document in the db. Raises an exception if the document is not created properly.
24 25 26 |
# File 'lib/couchrest/model/persistence.rb', line 24 def create!( = {}) self.class.fail_validate!(self) unless self.create() end |
#destroy ⇒ Object
Deletes the document from the database. Runs the :destroy callbacks.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/couchrest/model/persistence.rb', line 58 def destroy _run_destroy_callbacks do result = database.delete_doc(self) if result['ok'] @_destroyed = true self.freeze end result['ok'] end end |
#destroyed? ⇒ Boolean
69 70 71 |
# File 'lib/couchrest/model/persistence.rb', line 69 def destroyed? !!@_destroyed end |
#persisted? ⇒ Boolean
73 74 75 |
# File 'lib/couchrest/model/persistence.rb', line 73 def persisted? !new? && !destroyed? end |
#reload ⇒ Object
Reloads the attributes of this object from the database. It doesn’t override custom instance variables.
Returns self.
94 95 96 97 |
# File 'lib/couchrest/model/persistence.rb', line 94 def reload prepare_all_attributes(database.get(id), :directly_set_attributes => true) self end |
#save(options = {}) ⇒ Object
Trigger the callbacks (before, after, around) and save the document
46 47 48 |
# File 'lib/couchrest/model/persistence.rb', line 46 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.
52 53 54 55 |
# File 'lib/couchrest/model/persistence.rb', line 52 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
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/couchrest/model/persistence.rb', line 30 def update( = {}) raise "Cannot save a destroyed document!" if destroyed? raise "Calling #{self.class.name}#update on document that has not been created!" if new? return false unless perform_validations() return true if !self.disable_dirty && !self.changed? _run_update_callbacks do _run_save_callbacks do result = database.save_doc(self) ret = result["ok"] == true @changed_attributes.clear if ret && @changed_attributes ret 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
85 86 87 88 |
# File 'lib/couchrest/model/persistence.rb', line 85 def update_attributes(hash) update_attributes_without_saving hash save end |