Module: CouchbaseOrm::Persistence
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#delete(with_cas: false, **options) ⇒ Object
Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can’t be persisted).
-
#destroy(with_cas: false, **options) ⇒ Object
(also: #destroy!)
Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can’t be persisted).
-
#destroyed? ⇒ Boolean
Returns true if this object has been destroyed, otherwise returns false.
-
#new_record? ⇒ Boolean
(also: #new?)
Returns true if this object hasn’t been saved yet – that is, a record for the object doesn’t exist in the database yet; otherwise, returns false.
-
#persisted? ⇒ Boolean
(also: #exists?)
Returns true if the record is persisted, i.e.
-
#reload ⇒ Object
Reloads the record from the database.
-
#save(**options) ⇒ Object
Saves the model.
-
#save! ⇒ Object
Saves the model.
-
#touch(**options) ⇒ Object
Updates the TTL of the document.
-
#update(hash) ⇒ Object
(also: #update_attributes)
Updates the attributes of the model from the passed-in hash and saves the record.
-
#update!(hash) ⇒ Object
(also: #update_attributes!)
Updates its receiver just like #update but calls #save! instead of
save, so an exception is raised if the record is invalid and saving will fail. -
#update_attribute(name, value) ⇒ Object
Updates a single attribute and saves the record.
Instance Method Details
#delete(with_cas: false, **options) ⇒ Object
Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can’t be persisted). Returns the frozen instance.
The record is simply removed, no callbacks are executed.
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/couchbase-orm/persistence.rb', line 100 def delete(with_cas: false, **) [:cas] = .cas if with_cas self.class.bucket.delete(.key, ) .key = nil @id = nil clear_changes_information self.freeze self end |
#destroy(with_cas: false, **options) ⇒ Object Also known as: destroy!
Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can’t be persisted).
There’s a series of callbacks associated with #destroy.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/couchbase-orm/persistence.rb', line 116 def destroy(with_cas: false, **) return self if destroyed? raise 'model not persisted' unless persisted? run_callbacks :destroy do destroy_associations! [:cas] = .cas if with_cas self.class.bucket.delete(.key, ) .key = nil @id = nil clear_changes_information freeze end end |
#destroyed? ⇒ Boolean
Returns true if this object has been destroyed, otherwise returns false.
62 63 64 |
# File 'lib/couchbase-orm/persistence.rb', line 62 def destroyed? !!(.cas && .key.nil?) end |
#new_record? ⇒ Boolean Also known as: new?
Returns true if this object hasn’t been saved yet – that is, a record for the object doesn’t exist in the database yet; otherwise, returns false.
56 57 58 |
# File 'lib/couchbase-orm/persistence.rb', line 56 def new_record? .cas.nil? && .key.nil? end |
#persisted? ⇒ Boolean Also known as: exists?
Returns true if the record is persisted, i.e. it’s not a new record and it was not destroyed, otherwise returns false.
68 69 70 71 |
# File 'lib/couchbase-orm/persistence.rb', line 68 def persisted? # Changed? is provided by ActiveModel::Dirty !!.key end |
#reload ⇒ Object
Reloads the record from the database.
This method finds record by its key and modifies the receiver in-place:
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/couchbase-orm/persistence.rb', line 164 def reload key = .key raise "unable to reload, model not persisted" unless key resp = self.class.bucket.get(key, quiet: false, extended: true) @__attributes__ = ::ActiveSupport::HashWithIndifferentAccess.new(resp.value) .key = resp.key .cas = resp.cas reset_associations clear_changes_information self end |
#save(**options) ⇒ Object
Saves the model.
If the model is new, a record gets created in the database, otherwise the existing record gets updated.
78 79 80 81 |
# File 'lib/couchbase-orm/persistence.rb', line 78 def save(**) raise "Cannot save a destroyed document!" if destroyed? self.new_record? ? _create_record(**) : _update_record(**) end |
#save! ⇒ Object
Saves the model.
If the model is new, a record gets created in the database, otherwise the existing record gets updated.
By default, #save! always runs validations. If any of them fail CouchbaseOrm::Error::RecordInvalid gets raised, and the record won’t be saved.
90 91 92 93 |
# File 'lib/couchbase-orm/persistence.rb', line 90 def save! self.class.fail_validate!(self) unless self.save self end |
#touch(**options) ⇒ Object
Updates the TTL of the document
179 180 181 182 183 |
# File 'lib/couchbase-orm/persistence.rb', line 179 def touch(**) res = self.class.bucket.touch(.key, async: false, **) .cas = resp.cas self end |
#update(hash) ⇒ Object Also known as: update_attributes
Updates the attributes of the model from the passed-in hash and saves the record. If the object is invalid, the saving will fail and false will be returned.
147 148 149 150 |
# File 'lib/couchbase-orm/persistence.rb', line 147 def update(hash) assign_attributes(hash) save end |
#update!(hash) ⇒ Object Also known as: update_attributes!
Updates its receiver just like #update but calls #save! instead of save, so an exception is raised if the record is invalid and saving will fail.
155 156 157 158 |
# File 'lib/couchbase-orm/persistence.rb', line 155 def update!(hash) assign_attributes(hash) # Assign attributes is provided by ActiveModel::AttributeAssignment save! end |
#update_attribute(name, value) ⇒ Object
Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records. Also note that
-
Validation is skipped.
-
Callbacks are invoked.
140 141 142 143 |
# File 'lib/couchbase-orm/persistence.rb', line 140 def update_attribute(name, value) public_send(:"#{name}=", value) changed? ? save(validate: false) : true end |