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

Instance Method Details

#delete(**opts) ⇒ Boolean

Destroy resource by sending a DELETE request.

Deleting a resource is a synchronous operation.

Returns:

See Also:



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

Returns:

  • (undefined)

Raises:

See Also:



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.

Returns:

  • (Boolean)

    True if resource is newly created, false otherwise.



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

Examples:

Newly created resource:

user = User.new name: "John"
user.persisted? # => false
user.save
user.persisted? # => true

Modified resource:

user2 = User.find 5
user2.persisted? # => true
user2.name = 'Amy'
user2.persisted? # => true
user2.save
user2.persisted? # => true

Returns:

  • (Boolean)

    True if resource has been saved



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.

Returns:

  • (Boolean)

    True if save operation was successful, false otherwise.

See Also:



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.

Parameters:

  • opts (Hash)

    Hash with additional options.

Options Hash (**opts):

  • :data (Hash)

    Data to send to remote service. Default will be resource attributes.

Raises:

See Also:



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.

Parameters:

  • attrs (Hash)

    Hash with attributes to write.

  • opts (Hash)

    Options passed to `save`.

Returns:

  • (Boolean)

    True if save operation was successful, false otherwise.

See Also:



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.

Parameters:

  • attrs (Hash)

    Hash with attributes to write.

  • opts (Hash)

    Options passed to `save!`.

Raises:

See Also:



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