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

Instance Method Details

#create(attributes) ⇒ FulfilApi::Resource

Creates a resource with the given attributes and saves it.

Examples:

Creating a resource

resource.create(reference: "MK123")

Parameters:

  • attributes (Hash)

    The attributes to assign to the resource.

Returns:



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.

Examples:

Creating a resource with error raising

resource.create(reference: "MK123")

Parameters:

  • attributes (Hash)

    The attributes to assign to the resource.

Returns:

Raises:



96
97
98
99
# File 'lib/fulfil_api/resource/persistable.rb', line 96

def create!(attributes)
  assign_attributes(attributes)
  save!
end

#saveFulfilApi::Resource?

Saves the current resource, rescuing any errors that occur and handling them based on error type.

Examples:

Saving a resource

resource.save

Returns:

Raises:



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.

Examples:

Saving a resource with error raising

resource.save!

Returns:

Raises:



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.

Examples:

Updating a resource

resource.update(reference: "MK123")

Parameters:

  • attributes (Hash)

    The attributes to assign to the resource.

Returns:



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.

Examples:

Updating a resource with error raising

resource.update!(reference: "MK123")

Parameters:

  • attributes (Hash)

    The attributes to assign to the resource.

Returns:

Raises:



153
154
155
156
# File 'lib/fulfil_api/resource/persistable.rb', line 153

def update!(attributes)
  assign_attributes(attributes)
  save!
end