module Acfs::Resource::Persistence
Allow to track the persistence state of a model.
Public Instance Methods
Source
# File lib/acfs/resource/persistence.rb, line 155 def delete(**opts) delete!(**opts) true rescue Acfs::Error false end
@api public
Destroy resource by sending a DELETE request.
Deleting a resource is a synchronous operation.
@return [Boolean] @see delete!
Source
# 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
@raise [Acfs::ErroneousResponse]
If remote service respond with not successful response.
@return [undefined] @see delete
Source
# File lib/acfs/resource/persistence.rb, line 42 def new? !loaded? end
@api public
Return true if model is a new record and was not saved yet.
@return [Boolean] True if resource is newly created,
false otherwise.
Source
# File lib/acfs/resource/persistence.rb, line 31 def persisted? !new? end
@api public
Check if the model is persisted. A model is persisted if it is saved after being created
@example Newly created resource:
user = User.new name: "John" user.persisted? # => false user.save user.persisted? # => true
@example Modified resource:
user2 = User.find 5 user2.persisted? # => true user2.name = 'Amy' user2.persisted? # => true user2.save user2.persisted? # => true
@return [Boolean] True if resource has been saved
Source
# File lib/acfs/resource/persistence.rb, line 60 def save(**opts) save!(**opts) true rescue Acfs::Error false end
@api public
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.
@return [Boolean] True if save operation was successful,
false otherwise.
@see save!
See {#save!} for available options.
Source
# 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
@api public
Saves the resource. Raises an error if something happens.
Saving a resource is a synchronous operation.
@param opts [Hash] Hash with additional options. @option opts [Hash] :data Data to send to remote service.
Default will be resource attributes.
@raise [Acfs::InvalidResource]
If remote services respond with 422 response. Will fill errors with data from response
@raise [Acfs::ErroneousResponse]
If remote service respond with not successful response.
@see save
Source
# File lib/acfs/resource/persistence.rb, line 112 def update_attributes(attrs, **opts) check_loaded!(**opts) self.attributes = attrs save(**opts) end
@api public
Update attributes with given data and save resource.
Saving a resource is a synchronous operation.
@param attrs [Hash] Hash with attributes to write. @param opts [Hash] Options passed to ‘save`.
@return [Boolean]
True if save operation was successful, false otherwise.
@see save
@see attributes= @see update_attributes!
Source
# File lib/acfs/resource/persistence.rb, line 139 def update_attributes!(attrs, **opts) check_loaded! opts self.attributes = attrs save!(**opts) end
@api public
Update attributes with given data and save resource.
Saving a resource is a synchronous operation.
@param [Hash] attrs Hash with attributes to write. @param [Hash] opts Options passed to ‘save!`.
@raise [Acfs::InvalidResource]
If remote services respond with 422 response. Will fill errors with data from response
@raise [Acfs::ErroneousResponse]
If remote service respond with not successful response.
@see save!
@see attributes= @see update_attributes
Private Instance Methods
Source
# File lib/acfs/resource/persistence.rb, line 187 def attributes_for_url(action) arguments_for_url = self.class.location(action: action).arguments attributes.slice(*arguments_for_url) end
Source
# File lib/acfs/resource/persistence.rb, line 252 def check_loaded!(opts = {}) return if loaded? || opts[:force] raise ::Acfs::ResourceNotLoaded.new resource: self end
Source
# File lib/acfs/resource/persistence.rb, line 247 def update_with(data) self.attributes = data loaded! end