Module: FulfilApi::Resource::Persistable
- Extended by:
- ActiveSupport::Concern
- Included in:
- FulfilApi::Resource
- Defined in:
- lib/fulfil_api/resource/persistable.rb
Overview
The Persistable module provides methods for saving and updating resources
in the Fulfil API. It defines both instance and class methods for persisting
changes to resources.
This module handles common actions like saving and updating a resource,
including error handling for different types of API errors.
Instance Method Summary collapse
-
#create(attributes) ⇒ FulfilApi::Resource
Creates a resource with the given attributes and saves it.
-
#create!(attributes) ⇒ FulfilApi::Resource
Creates a resource with the given attributes and saves it, raising an error if saving fails.
-
#save ⇒ FulfilApi::Resource?
Saves the current resource, rescuing any errors that occur and handling them based on error type.
-
#save! ⇒ FulfilApi::Resource
Saves the current resource, raising an error if it cannot be saved.
-
#update(attributes) ⇒ FulfilApi::Resource
Updates the resource with the given attributes and saves it.
-
#update!(attributes) ⇒ FulfilApi::Resource
Updates the resource with the given attributes and saves it, raising an error if saving fails.
Instance Method Details
#create(attributes) ⇒ FulfilApi::Resource
Creates a resource with the given attributes and saves it.
83 84 85 86 |
# File 'lib/fulfil_api/resource/persistable.rb', line 83 def create(attributes) assign_attributes(attributes) save end |
#create!(attributes) ⇒ FulfilApi::Resource
Creates a resource with the given attributes and saves it, raising an error if saving fails.
96 97 98 99 |
# File 'lib/fulfil_api/resource/persistable.rb', line 96 def create!(attributes) assign_attributes(attributes) save! end |
#save ⇒ FulfilApi::Resource?
Saves the current resource, rescuing any errors that occur and handling them based on error type.
108 109 110 111 112 |
# File 'lib/fulfil_api/resource/persistable.rb', line 108 def save save! rescue FulfilApi::Error => e handle_exception(e) end |
#save! ⇒ FulfilApi::Resource
Saves the current resource, raising an error if it cannot be saved.
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/fulfil_api/resource/persistable.rb', line 121 def save! errors.clear if id.present? FulfilApi.client.put("/model/#{model_name}/#{id}", body: to_h) else FulfilApi.client.post("/model/#{model_name}", body: to_h) end self end |
#update(attributes) ⇒ FulfilApi::Resource
Updates the resource with the given attributes and saves it.
140 141 142 143 |
# File 'lib/fulfil_api/resource/persistable.rb', line 140 def update(attributes) assign_attributes(attributes) save end |
#update!(attributes) ⇒ FulfilApi::Resource
Updates the resource with the given attributes and saves it, raising an error if saving fails.
153 154 155 156 |
# File 'lib/fulfil_api/resource/persistable.rb', line 153 def update!(attributes) assign_attributes(attributes) save! end |