lib/acfs/model/persistence.rb in acfs-0.19.0 vs lib/acfs/model/persistence.rb in acfs-0.20.0.dev.b181
- old
+ new
@@ -24,21 +24,21 @@
# user2.name = 'Amy'
# user2.persisted? # => false
# user2.save
# user2.persisted? # => true
#
- # @return [TrueClass, FalseClass] True if resource has no changes and is not newly created, false otherwise.
+ # @return [Boolean] True if resource has no changes and is not newly created, false otherwise.
#
def persisted?
!new? && !changed?
end
# @api public
#
# Return true if model is a new record and was not saved yet.
#
- # @return [TrueClass, FalseClass] True if resource is newly created, false otherwise.
+ # @return [Boolean] True if resource is newly created, false otherwise.
#
def new?
read_attribute(:id).nil?
end
alias :new_record? :new?
@@ -50,11 +50,11 @@
# 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 [TrueClass, FalseClass] True if save operation was successful, false otherwise.
+ # @return [Boolean] True if save operation was successful, false otherwise.
# @see #save! See #save! for available options.
#
def save(*args)
save! *args
true
@@ -77,26 +77,71 @@
# If remote service respond with not successful response.
#
# @see #save
#
def save!(opts = {})
- #raise ::Acfs::InvalidResource errors: errors.to_a unless valid?
-
opts[:data] = attributes unless opts[:data]
operation (new? ? :create : :update), opts do |data|
update_with data
end
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`.
+ #
+ # @return [Boolean] True if save operation was successful, false otherwise.
+ #
+ # @see #save
+ # @see #attributes=
+ # @see #update_attributes!
+ #
+ 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
+ #
+ def update_attributes!(attrs, opts = {})
+ check_loaded! opts
+
+ self.attributes = attrs
+ save! opts
+ end
+
+ # @api public
+ #
# Destroy resource by sending a DELETE request.
#
# Deleting a resource is a synchronous operation.
#
- # @return [TrueClass, FalseClass]
+ # @return [Boolean]
# @see #delete!
#
def delete(opts = {})
delete! opts
true
@@ -183,9 +228,13 @@
private
def update_with(data)
self.attributes = data
loaded!
+ end
+
+ def check_loaded!(opts = {})
+ raise ResourceNotLoaded, resource: self unless loaded? or opts[:force]
end
end
end
end