Module: Acfs::Resource::Persistence
- Extended by:
- ActiveSupport::Concern
- Included in:
- Acfs::Resource
- Defined in:
- lib/acfs/resource/persistence.rb
Overview
Allow to track the persistence state of a model.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#delete(**opts) ⇒ Boolean
Destroy resource by sending a DELETE request.
- #delete!(**opts) ⇒ undefined
-
#new? ⇒ Boolean
(also: #new_record?)
Return true if model is a new record and was not saved yet.
-
#persisted? ⇒ Boolean
Check if the model is persisted.
-
#save(**opts) ⇒ Boolean
Saves the resource.
-
#save!(**opts) ⇒ Object
Saves the resource.
-
#update_attributes(attrs, **opts) ⇒ Boolean
Update attributes with given data and save resource.
-
#update_attributes!(attrs, **opts) ⇒ Object
Update attributes with given data and save resource.
Instance Method Details
#delete(**opts) ⇒ Boolean
Destroy resource by sending a DELETE request.
Deleting a resource is a synchronous operation.
155 156 157 158 159 160 |
# File 'lib/acfs/resource/persistence.rb', line 155 def delete(**opts) delete!(**opts) true rescue Acfs::Error false end |
#delete!(**opts) ⇒ undefined
175 176 177 178 179 180 181 182 183 |
# File 'lib/acfs/resource/persistence.rb', line 175 def delete!(**opts) opts[:params] ||= {} opts[:params] = attributes_for_url(:delete).merge opts[:params] operation(:delete, **opts) do |data| update_with data freeze end end |
#new? ⇒ Boolean Also known as: new_record?
Return true if model is a new record and was not saved yet.
42 43 44 |
# File 'lib/acfs/resource/persistence.rb', line 42 def new? !loaded? end |
#persisted? ⇒ Boolean
Check if the model is persisted. A model is persisted if it is saved after being created
31 32 33 |
# File 'lib/acfs/resource/persistence.rb', line 31 def persisted? !new? end |
#save(**opts) ⇒ Boolean
Saves the resource.
It will PUT to the service to update the resource or send a POST to create a new one if the resource is new.
Saving a resource is a synchronous operation.
60 61 62 63 64 65 |
# File 'lib/acfs/resource/persistence.rb', line 60 def save(**opts) save!(**opts) true rescue Acfs::Error false end |
#save!(**opts) ⇒ Object
Saves the resource. Raises an error if something happens.
Saving a resource is a synchronous operation.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/acfs/resource/persistence.rb', line 85 def save!(**opts) opts[:data] = attributes unless opts[:data] operation((new? ? :create : :update), **opts) do |data| update_with data end rescue ::Acfs::InvalidResource => e self.remote_errors = e.errors raise e end |
#update_attributes(attrs, **opts) ⇒ Boolean
Update attributes with given data and save resource.
Saving a resource is a synchronous operation.
112 113 114 115 116 117 |
# File 'lib/acfs/resource/persistence.rb', line 112 def update_attributes(attrs, **opts) check_loaded!(**opts) self.attributes = attrs save(**opts) end |
#update_attributes!(attrs, **opts) ⇒ Object
Update attributes with given data and save resource.
Saving a resource is a synchronous operation.
139 140 141 142 143 144 |
# File 'lib/acfs/resource/persistence.rb', line 139 def update_attributes!(attrs, **opts) check_loaded! opts self.attributes = attrs save!(**opts) end |