Class: ErpIntegration::Resource

Inherits:
Object
  • Object
show all
Includes:
ErpIntegration::Resources::Persistence, ErpIntegration::Resources::Validations
Defined in:
lib/erp_integration/resource.rb

Overview

The ‘ErpIntegration::Resource` is a generic, re-usable model for third-party sources.

For the ‘ErpIntegration::Resource`, we’re using the adapter pattern. Meaning; the ‘ErpIntegration::Resource` is a facade that delegates the actual work to an adapter.

Every class that inherits from the ‘ErpIntegration::Resource`, will be able to configure a designated adapter. This allows configuring an adapter per resource to maximize the flexibility.

To add a new resource, follow these steps:

1. Add a new `attr_writer` in the `ErpIntegration::Configuration` class.
2. Add a new method to the `ErpIntegration::Configuration` that sets up the
   default adapter.
3. Create a new generic resource model in the `lib/erp_integration` folder.
4. Create a new pluralized folder name in the `lib/erp_integration` folder
   (e.g. `orders` for the `Order` resource).
5. Create a new adapter class prefixed with the adapter's name
   (e.g. `FulfilOrder` for the `Order` resource in the `lib/erp_integration/orders` folder).

Examples:

ErpIntegration.configure do |config|
  config.order_adapter = :fulfil
end

$ ErpIntegration::SalesOrder.adapter
=> #<ErpIntegration::SalesOrders::FulfilOrder />

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ErpIntegration::Resources::Persistence

#destroy, included, #persisted?, #update

Methods included from ErpIntegration::Resources::Validations

#errors, #valid?, #validate_with

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



37
38
39
40
# File 'lib/erp_integration/resource.rb', line 37

def initialize(attributes = {})
  @raw_api_response = attributes
  assign_attributes(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **kwargs, &block) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/erp_integration/resource.rb', line 110

def method_missing(method_name, *args, &block)
  if adapter.respond_to?(method_name)
    adapter.send(method_name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#raw_api_responseObject

Returns the value of attribute raw_api_response.



35
36
37
# File 'lib/erp_integration/resource.rb', line 35

def raw_api_response
  @raw_api_response
end

Class Method Details

.adapterClass

Dynamically defines and loads the adapter for the class inheriting from the ‘ErpIntegration::Resource`.

Returns:

  • (Class)

    The adapter class for the resource.



74
75
76
77
78
79
# File 'lib/erp_integration/resource.rb', line 74

def adapter
  return @adapter if defined?(@adapter)

  require_relative File.join(File.dirname(__FILE__), "#{adapter_path}.rb")
  @adapter = adapter_klass.constantize.new(self)
end

.adapter_klassString

Dynamically exposes the adapter class to the resource.

Returns:

  • (String)

    the adapter class as a string



83
84
85
# File 'lib/erp_integration/resource.rb', line 83

def adapter_klass
  "ErpIntegration::#{adapter_path.classify}"
end

.adapter_pathString

Provides a relative path to the adapter for the resource.

Returns:

  • (String)

    the path to the adapter



89
90
91
# File 'lib/erp_integration/resource.rb', line 89

def adapter_path
  "#{adapter_type}/resources/#{resource_name}"
end

.adapter_typeString

Retrieves the adapter type for the resource from the global configuration.

Returns:

  • (String)

    the adapter type for the resource



95
96
97
# File 'lib/erp_integration/resource.rb', line 95

def adapter_type
  ErpIntegration.config.send("#{resource_name}_adapter").to_s
end

.resource_nameString

Derives the name of the resource from the class name.

Returns:

  • (String)

    the resource name



137
138
139
# File 'lib/erp_integration/resource.rb', line 137

def resource_name
  name.split('::').last.underscore
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

The ‘respond_to_missing?` complements the `method_missing` method.

Parameters:

  • method_name (Symbol)

    The name of the missing method.

  • include_private (Boolean) (defaults to: false)

    Whether private methods should be checked too (defaults to false).

Returns:

  • (Boolean)


131
132
133
# File 'lib/erp_integration/resource.rb', line 131

def respond_to_missing?(method_name, include_private = false)
  adapter.respond_to?(method_name) || super
end

Instance Method Details

#assign_attributes(attributes = {}) ⇒ Hash

Allows assigning attributes to an resource instance.

Parameters:

  • attributes (Hash) (defaults to: {})

    A list of attributes for the resource.

Returns:

  • (Hash)

    The list of assigned attributes.



45
46
47
48
49
# File 'lib/erp_integration/resource.rb', line 45

def assign_attributes(attributes = {})
  attributes.each_pair do |name, value|
    public_send(:"#{name}=", value) if respond_to?(:"#{name}=")
  end
end