Module: Cardiac::Model::Persistence
Overview
Cardiac::Model persistence methods. Most of this has been “borrowed” from ActiveRecord.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#delete ⇒ Object
Performs a DELETE on the remote resource if the record is not new, marks it as destroyed, then freezes the attributes.
-
#destroy ⇒ Object
Delegates to delete, except it raises a
ReadOnlyRecorderror if the record is read-only. -
#destroy! ⇒ Object
Delegates to destroy, but raises
RecordNotDestroyedif checks fail. -
#destroyed? ⇒ Boolean
Returns true if this object has been destroyed, otherwise returns false.
-
#new_record? ⇒ Boolean
Returns true if this object hasn’t been saved yet – that is, a record for the object doesn’t exist in the data store yet; otherwise, returns false.
-
#persisted? ⇒ Boolean
Returns true if the record is persisted, i.e.
-
#reload(*args) ⇒ Object
Reloads the attributes of this object from the remote.
-
#save ⇒ Object
Delegates to create_or_update, but rescues validation exceptions to return false.
-
#save! ⇒ Object
Delegates to create_or_update, but raises
RecordNotSavedif validations fail. -
#update(attributes) ⇒ Object
(also: #update_attributes)
Updates the attributes of the model from the passed-in hash and saves the record.
-
#update!(attributes) ⇒ Object
(also: #update_attributes!)
Updates its receiver just like
updatebut callssave!instead ofsave, so an exception is raised if the record is invalid. -
#update_attribute(name, value) ⇒ Object
Updates a single attribute and saves the record.
Instance Method Details
#delete ⇒ Object
Performs a DELETE on the remote resource if the record is not new, marks it as destroyed, then freezes the attributes.
42 43 44 45 |
# File 'lib/cardiac/model/persistence.rb', line 42 def delete delete_record freeze end |
#destroy ⇒ Object
Delegates to delete, except it raises a ReadOnlyRecord error if the record is read-only.
48 49 50 51 |
# File 'lib/cardiac/model/persistence.rb', line 48 def destroy raise ReadOnlyRecord if readonly? delete end |
#destroy! ⇒ Object
Delegates to destroy, but raises RecordNotDestroyed if checks fail.
54 55 56 |
# File 'lib/cardiac/model/persistence.rb', line 54 def destroy! destroy || raise(RecordNotDestroyed) end |
#destroyed? ⇒ Boolean
Returns true if this object has been destroyed, otherwise returns false.
30 31 32 |
# File 'lib/cardiac/model/persistence.rb', line 30 def destroyed? @destroyed end |
#new_record? ⇒ Boolean
Returns true if this object hasn’t been saved yet – that is, a record for the object doesn’t exist in the data store yet; otherwise, returns false.
25 26 27 |
# File 'lib/cardiac/model/persistence.rb', line 25 def new_record? @new_record end |
#persisted? ⇒ Boolean
Returns true if the record is persisted, i.e. it’s not a new record and it was not destroyed, otherwise returns false.
36 37 38 |
# File 'lib/cardiac/model/persistence.rb', line 36 def persisted? !(new_record? || destroyed?) end |
#reload(*args) ⇒ Object
Reloads the attributes of this object from the remote. Any (optional) arguments are passed to find when reloading.
72 73 74 75 76 |
# File 'lib/cardiac/model/persistence.rb', line 72 def reload(*args) fresh_object = self.class.find(self.id, *args) @attributes.update(fresh_object.instance_variable_get('@attributes')) self end |
#save ⇒ Object
Delegates to create_or_update, but rescues validation exceptions to return false.
59 60 61 62 63 |
# File 'lib/cardiac/model/persistence.rb', line 59 def save(*) create_or_update rescue RecordInvalid false end |
#save! ⇒ Object
Delegates to create_or_update, but raises RecordNotSaved if validations fail.
66 67 68 |
# File 'lib/cardiac/model/persistence.rb', line 66 def save!(*) create_or_update || raise(RecordNotSaved) end |
#update(attributes) ⇒ 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.
96 97 98 99 |
# File 'lib/cardiac/model/persistence.rb', line 96 def update(attributes) assign_attributes(attributes) save end |
#update!(attributes) ⇒ 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.
105 106 107 108 |
# File 'lib/cardiac/model/persistence.rb', line 105 def update!(attributes) assign_attributes(attributes) 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.
-
updated_at/updated_on column is updated if that column is available.
-
Updates all the attributes that are dirty in this object.
This method raises an OperationFailError if the attribute is marked as readonly.
87 88 89 90 91 92 |
# File 'lib/cardiac/model/persistence.rb', line 87 def update_attribute(name, value) name = name.to_s verify_readonly_attribute(name) send("#{name}=", value) save(validate: false) end |