Class: ErpIntegration::Resource
- Inherits:
-
Object
- Object
- ErpIntegration::Resource
- 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).
Direct Known Subclasses
BillOfMaterial, BillOfMaterialInput, BillOfMaterialOutput, BoxType, Carrier, CarrierService, ChannelListing, Country, CustomerShipment, CustomerShipmentReturn, GiftCard, InternalShipment, Location, PartyAddress, Product, ProductCategory, ProductOption, ProductTemplate, ProductionOrder, PurchaseOrder, PurchaseOrderLine, PurchaseProductSupplier, PurchaseRequest, SalesLineOption, SalesOrder, SalesOrderLine, SalesReturnReason, StockBinTransfer, StockMove, SupplierShipment, Task, TrackingNumber, Webhook
Instance Attribute Summary collapse
-
#raw_api_response ⇒ Object
Returns the value of attribute raw_api_response.
Class Method Summary collapse
-
.adapter ⇒ Class
Dynamically defines and loads the adapter for the class inheriting from the ‘ErpIntegration::Resource`.
-
.adapter_klass ⇒ String
Dynamically exposes the adapter class to the resource.
-
.adapter_path ⇒ String
Provides a relative path to the adapter for the resource.
-
.adapter_type ⇒ String
Retrieves the adapter type for the resource from the global configuration.
-
.method_missing(method_name, *args, **kwargs, &block) ⇒ Object
Due to ‘method_missing` we’re able to delegate all the work automatically to the adapter.
-
.resource_name ⇒ String
Derives the name of the resource from the class name.
-
.respond_to_missing?(method_name, include_private = false) ⇒ Boolean
The ‘respond_to_missing?` complements the `method_missing` method.
Instance Method Summary collapse
-
#assign_attributes(attributes = {}) ⇒ Hash
Allows assigning attributes to an resource instance.
-
#initialize(attributes = {}) ⇒ Resource
constructor
A new instance of Resource.
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 |
Instance Attribute Details
#raw_api_response ⇒ Object
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
.adapter ⇒ Class
Dynamically defines and loads the adapter for the class inheriting from the ‘ErpIntegration::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_klass ⇒ String
Dynamically exposes the adapter class to the resource.
83 84 85 |
# File 'lib/erp_integration/resource.rb', line 83 def adapter_klass "ErpIntegration::#{adapter_path.classify}" end |
.adapter_path ⇒ String
Provides a relative path to the adapter for the resource.
89 90 91 |
# File 'lib/erp_integration/resource.rb', line 89 def adapter_path "#{adapter_type}/resources/#{resource_name}" end |
.adapter_type ⇒ String
Retrieves the adapter type for the resource from the global configuration.
95 96 97 |
# File 'lib/erp_integration/resource.rb', line 95 def adapter_type ErpIntegration.config.send("#{resource_name}_adapter").to_s end |
.method_missing(method_name, *args, **kwargs, &block) ⇒ Object
Due to ‘method_missing` we’re able to delegate all the work automatically to the adapter. However, if the method doesn’t exist on the adapter, the ‘NoMethodError` is still being raised.
108 109 110 111 112 113 114 |
# File 'lib/erp_integration/resource.rb', line 108 def method_missing(method_name, *args, **kwargs, &block) if adapter.respond_to?(method_name) adapter.send(method_name, *args, **kwargs, &block) else super end end |
.resource_name ⇒ String
Derives the name of the resource from the class name.
125 126 127 |
# File 'lib/erp_integration/resource.rb', line 125 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.
119 120 121 |
# File 'lib/erp_integration/resource.rb', line 119 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.
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 |