Module: ActiveRemote::Persistence
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#delete ⇒ Object
Deletes the record from the service (the service determines if the record is hard or soft deleted) and freezes this instance to indicate that no changes should be made (since they can’t be persisted).
-
#delete! ⇒ Object
Deletes the record from the service (the service determines if the record is hard or soft deleted) and freezes this instance to indicate that no changes should be made (since they can’t be persisted).
-
#destroy ⇒ Object
Destroys (hard deletes) the record from the service and freezes this instance to indicate that no changes should be made (since they can’t be persisted).
-
#destroy! ⇒ Object
Destroys (hard deletes) the record from the service and freezes this instance to indicate that no changes should be made (since they can’t be persisted).
-
#has_errors? ⇒ Boolean
Returns true if the record has errors; otherwise, returns false.
-
#instantiate(new_attributes) ⇒ Object
Instantiate a record with the given remote attributes.
-
#new_record? ⇒ Boolean
Returns true if the remote record hasn’t been saved yet; otherwise, returns false.
-
#persisted? ⇒ Boolean
Returns true if the remote record has been saved; otherwise, returns false.
-
#readonly! ⇒ Object
Sets the instance to be a readonly object.
-
#readonly? ⇒ Boolean
Returns true if the remote class or remote record is readonly; otherwise, returns false.
-
#remote(endpoint, request_args = scope_key_hash) ⇒ Object
Executes a remote call on the current object and serializes it’s attributes and errors from the response.
-
#save(*args) ⇒ Object
Saves the remote record.
-
#save!(*args) ⇒ Object
Saves the remote record.
-
#success? ⇒ Boolean
Returns true if the record doesn’t have errors; otherwise, returns false.
-
#update_attribute(name, value) ⇒ Object
Updates a single attribute and saves the record.
-
#update_attributes(attributes) ⇒ Object
(also: #update)
Updates the attributes of the remote record from the passed-in hash and saves the remote record.
-
#update_attributes!(attributes) ⇒ Object
(also: #update!)
Updates the attributes of the remote record from the passed-in hash and saves the remote record.
Instance Method Details
#delete ⇒ Object
Deletes the record from the service (the service determines if the record is hard or soft deleted) and freezes this instance to indicate that no changes should be made (since they can’t be persisted). If the record was not deleted, it will have error messages indicating what went wrong. Returns the frozen instance.
77 78 79 80 81 82 83 84 85 |
# File 'lib/active_remote/persistence.rb', line 77 def delete raise ReadOnlyRemoteRecord if readonly? response = remote_call(:delete, scope_key_hash) add_errors(response.errors) if response.respond_to?(:errors) success? ? freeze : false end |
#delete! ⇒ Object
Deletes the record from the service (the service determines if the record is hard or soft deleted) and freezes this instance to indicate that no changes should be made (since they can’t be persisted). If the record was not deleted, an exception will be raised. Returns the frozen instance.
93 94 95 96 |
# File 'lib/active_remote/persistence.rb', line 93 def delete! delete raise ActiveRemoteError, errors.to_s if has_errors? end |
#destroy ⇒ Object
Destroys (hard deletes) the record from the service and freezes this instance to indicate that no changes should be made (since they can’t be persisted). If the record was not deleted, it will have error messages indicating what went wrong. Returns the frozen instance.
103 104 105 106 107 108 109 110 111 |
# File 'lib/active_remote/persistence.rb', line 103 def destroy raise ReadOnlyRemoteRecord if readonly? response = remote_call(:destroy, scope_key_hash) add_errors(response.errors) if response.respond_to?(:errors) success? ? freeze : false end |
#destroy! ⇒ Object
Destroys (hard deletes) the record from the service and freezes this instance to indicate that no changes should be made (since they can’t be persisted). If the record was not deleted, an exception will be raised. Returns the frozen instance.
118 119 120 121 |
# File 'lib/active_remote/persistence.rb', line 118 def destroy! destroy raise ActiveRemoteError, errors.to_s if has_errors? end |
#has_errors? ⇒ Boolean
Returns true if the record has errors; otherwise, returns false.
125 126 127 |
# File 'lib/active_remote/persistence.rb', line 125 def has_errors? respond_to?(:errors) && errors.present? end |
#instantiate(new_attributes) ⇒ Object
Instantiate a record with the given remote attributes. Generally used when retrieving records that already exist, so @new_record is set to false.
132 133 134 135 |
# File 'lib/active_remote/persistence.rb', line 132 def instantiate(new_attributes) new_attributes = self.class.build_from_rpc(new_attributes) init_with(new_attributes) end |
#new_record? ⇒ Boolean
Returns true if the remote record hasn’t been saved yet; otherwise, returns false.
140 141 142 |
# File 'lib/active_remote/persistence.rb', line 140 def new_record? @new_record end |
#persisted? ⇒ Boolean
Returns true if the remote record has been saved; otherwise, returns false.
146 147 148 |
# File 'lib/active_remote/persistence.rb', line 146 def persisted? !new_record? end |
#readonly! ⇒ Object
Sets the instance to be a readonly object
152 153 154 |
# File 'lib/active_remote/persistence.rb', line 152 def readonly! @readonly = true end |
#readonly? ⇒ Boolean
Returns true if the remote class or remote record is readonly; otherwise, returns false.
157 158 159 |
# File 'lib/active_remote/persistence.rb', line 157 def readonly? self.class.readonly? || @readonly end |
#remote(endpoint, request_args = scope_key_hash) ⇒ Object
Executes a remote call on the current object and serializes it’s attributes and errors from the response.
Defaults request args to the scope key hash (e.g., { guid: ‘ABC-123’ }) when none are given. Returns false if the response contained errors; otherwise, returns true.
167 168 169 170 171 172 |
# File 'lib/active_remote/persistence.rb', line 167 def remote(endpoint, request_args = scope_key_hash) response = remote_call(endpoint, request_args) assign_attributes_from_rpc(response) success? end |
#save(*args) ⇒ Object
Saves the remote record.
If it is a new record, it will be created through the service, otherwise the existing record gets updated.
The service will run any validations and if any of them fail, will return the record with error messages indicating what went wrong.
Also runs any before/after save callbacks that are defined.
184 185 186 187 188 |
# File 'lib/active_remote/persistence.rb', line 184 def save(*args) run_callbacks :save do create_or_update(*args) end end |
#save!(*args) ⇒ Object
Saves the remote record.
If it is a new record, it will be created through the service, otherwise the existing record gets updated.
The service will run any validations. If any of them fail (e.g. error messages are returned), an ActiveRemote::RemoteRecordNotSaved is raised.
Also runs any before/after save callbacks that are defined.
200 201 202 |
# File 'lib/active_remote/persistence.rb', line 200 def save!(*args) save(*args) || fail(RemoteRecordNotSaved, self) end |
#success? ⇒ Boolean
Returns true if the record doesn’t have errors; otherwise, returns false.
206 207 208 |
# File 'lib/active_remote/persistence.rb', line 206 def success? !has_errors? 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.
-
Updates all the attributes that are dirty in this object.
This method raises an ActiveRemote::ReadOnlyRemoteRecord if the attribute is marked as readonly.
219 220 221 222 223 224 225 |
# File 'lib/active_remote/persistence.rb', line 219 def update_attribute(name, value) raise ReadOnlyRemoteRecord if readonly? name = name.to_s send(:"#{name}=", value) save(validate: false) end |
#update_attributes(attributes) ⇒ Object Also known as: update
Updates the attributes of the remote record from the passed-in hash and saves the remote record. If the object is invalid, it will have error messages and false will be returned.
231 232 233 234 |
# File 'lib/active_remote/persistence.rb', line 231 def update_attributes(attributes) assign_attributes(attributes) save end |
#update_attributes!(attributes) ⇒ Object Also known as: update!
Updates the attributes of the remote record from the passed-in hash and saves the remote record. If the object is invalid, an ActiveRemote::RemoteRecordNotSaved is raised.
241 242 243 244 |
# File 'lib/active_remote/persistence.rb', line 241 def update_attributes!(attributes) assign_attributes(attributes) save! end |