Class: EPlat::Mapping::Base

Inherits:
Object
  • Object
show all
Includes:
RequestBodyRoot
Defined in:
lib/e_plat/mapping/base.rb

Direct Known Subclasses

EPlat::Mapping::Bigcommerce::V3::Metafield, EPlat::Mapping::Bigcommerce::V3::Order, EPlat::Mapping::Bigcommerce::V3::Order::LineItem, EPlat::Mapping::Bigcommerce::V3::Order::ShippingAddress, EPlat::Mapping::Bigcommerce::V3::Product, EPlat::Mapping::Bigcommerce::V3::Product::Image, EPlat::Mapping::Bigcommerce::V3::Product::Option, EPlat::Mapping::Bigcommerce::V3::Product::Variant, EPlat::Mapping::Bigcommerce::V3::Product::Variant::OptionValue, EPlat::Mapping::Bigcommerce::V3::ScriptTag, EPlat::Mapping::Bigcommerce::V3::Shop, EPlat::Mapping::Bigcommerce::V3::Webhook, Shopify::V202401::Metafield, Shopify::V202401::Order, Shopify::V202401::Order::ShippingAddress, Shopify::V202401::Product, Shopify::V202401::Product::Image, Shopify::V202401::Product::Variant, Shopify::V202401::ScriptTag, Shopify::V202401::Shop, Shopify::V202401::Webhook, Shopify::V202407::Metafield, Shopify::V202407::Order, Shopify::V202407::Order::ShippingAddress, Shopify::V202407::Product, Shopify::V202407::Product::Image, Shopify::V202407::Product::Option, Shopify::V202407::Product::Variant, Shopify::V202407::Product::Variant::OptionValue, Shopify::V202407::ScriptTag, Shopify::V202407::Shop, Shopify::V202407::Webhook, Shopify::V202501::Metafield, Shopify::V202501::Order, Shopify::V202501::Order::ShippingAddress, Shopify::V202501::Product, Shopify::V202501::Product::Image, Shopify::V202501::Product::Option, Shopify::V202501::Product::Variant, Shopify::V202501::Product::Variant::OptionValue, Shopify::V202501::ScriptTag, Shopify::V202501::Shop, Shopify::V202501::Webhook, Shopify::V202510::Metafield, Shopify::V202510::Order, Shopify::V202510::Order::ShippingAddress, Shopify::V202510::Product, Shopify::V202510::Product::Image, Shopify::V202510::Product::Option, Shopify::V202510::Product::Variant, Shopify::V202510::Product::Variant::OptionValue, Shopify::V202510::ScriptTag, Shopify::V202510::Shop, Shopify::V202510::Webhook, Woocommerce::V3::Metafield, Woocommerce::V3::Order, Woocommerce::V3::Order::BillingAddress, Woocommerce::V3::Order::LineItem, Woocommerce::V3::Order::ShippingAddress, Woocommerce::V3::Order::ShippingLine, Woocommerce::V3::Product, Woocommerce::V3::Product::Image, Woocommerce::V3::Product::Option, Woocommerce::V3::Product::Variant, Woocommerce::V3::Product::Variant::OptionValue, Woocommerce::V3::ScriptTag, Woocommerce::V3::Shop, Woocommerce::V3::Webhook

Constant Summary collapse

DEFAULT_VALUES =

“head”, auto_uninstall: true, …

{}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestBodyRoot

#include_root_in_request_body?

Constructor Details

#initialize(resource_instance) ⇒ Base

Returns a new instance of Base.



28
29
30
31
32
# File 'lib/e_plat/mapping/base.rb', line 28

def initialize(resource_instance)
  @resource = resource_instance

  init_virtual_collections if @resource.present?
end

Class Attribute Details

.virtual_collectionsObject

Returns the value of attribute virtual_collections.



10
11
12
# File 'lib/e_plat/mapping/base.rb', line 10

def virtual_collections
  @virtual_collections
end

Instance Attribute Details

#resourceObject

Returns the value of attribute resource.



5
6
7
# File 'lib/e_plat/mapping/base.rb', line 5

def resource
  @resource
end

#virtual_collectionObject

Returns the value of attribute virtual_collection.



5
6
7
# File 'lib/e_plat/mapping/base.rb', line 5

def virtual_collection
  @virtual_collection
end

Class Method Details

.virtual_collection(name, **options) ⇒ Object

virtual_collection :line_items, class: EPlat::Mapping::VirtualCollection::Bigcommerce::OrderLineItems, map_from: “consignments”



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/e_plat/mapping/base.rb', line 13

def virtual_collection(name, **options)
  self.virtual_collections ||= []

  self.virtual_collections.push({
    name: name,
    class: options[:class],
    map_from: options[:map_from]
  })

  define_method("init_#{name}") do |resource:, map_from:|
    instance_variable_set "@#{name}", "#{ options[:class] }".constantize.new(resource: resource, map_from: map_from)
  end
end

Instance Method Details

#aliasesObject

The below methods add support for getting/setting/converting via e_plat and native keys



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/e_plat/mapping/base.rb', line 71

def aliases #e.g. {description: "body_html", native_key: "e_plat_key", ...}
  @aliases ||= native_attribute_aliases.map do |alias_hash|
    if alias_hash[:alias_attribute]
      keys = alias_hash[:alias_attribute].values[0..1]
    elsif alias_hash[:existing_entry]
      keys = alias_hash[:existing_entry].values[0..1]
    else
      next
    end
    
    Hash[*keys]
  end.compact_blank.inject(&:merge) || {}
end

#e_plat_key_to(native_key) ⇒ Object

e.g. “body_html” => “description”



93
94
95
# File 'lib/e_plat/mapping/base.rb', line 93

def e_plat_key_to(native_key) #e.g.  "body_html" => "description"
  aliases[native_key]
end

#inspectObject



34
35
36
# File 'lib/e_plat/mapping/base.rb', line 34

def inspect
  "#<#{self.class} #{ self.instance_variable_names.join(' ') }>"
end

#keys_aliased_to_native_keysObject



85
86
87
# File 'lib/e_plat/mapping/base.rb', line 85

def keys_aliased_to_native_keys
  @e_plat_to_native_aliases ||= aliases.invert
end

#mappable_keysObject



107
108
109
# File 'lib/e_plat/mapping/base.rb', line 107

def mappable_keys
  native_attribute_aliases.map{|a| a.values.first[:e_plat_key]}.presence || :all
end

#mappingsObject

e.g. native_key, ‘body_html’ => ‘description’



111
112
113
# File 'lib/e_plat/mapping/base.rb', line 111

def mappings #e.g. {e_plat_key: native_key, 'body_html' => 'description'}
  @mapping ||= native_attribute_aliases.map{|a| a[:alias_attribute] }.compact_blank.inject(&:merge) || {}
end

#native_attribute_aliasesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/e_plat/mapping/base.rb', line 48

def native_attribute_aliases
  [
    # {
    #   alias_attribute: {native_key: "description",  e_plat_key: "body_html"}
    # }, {
    #   alias_attribute: {
    #   native_key: "description",  
    #   e_plat_key: "body_html", 
    #   custom_e_plat_getter: "-> (value) { value&.gsub(' ', '-') }",
    #   custom_native_setter: "-> (value) { value&.gsub('-', ' ') }" 
    # }
    # }, {
    #    existing_entry: {native_key: "id", e_plat_key: "id"}
    # } 
  ]
end

#native_attributesObject



44
45
46
# File 'lib/e_plat/mapping/base.rb', line 44

def native_attributes
  []
end

#native_key_to(e_plat_key) ⇒ Object

e.g. “description” => “body_html”



89
90
91
# File 'lib/e_plat/mapping/base.rb', line 89

def native_key_to(e_plat_key) #e.g. "description" => "body_html"
  aliases.invert[e_plat_key]
end

#native_setter(e_plat_key, value) ⇒ Object

str, proc.call(value) | nil



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/e_plat/mapping/base.rb', line 148

def native_setter(e_plat_key, value) # str, proc.call(value) | nil
  return unless keys_aliased_to_native_keys[e_plat_key]
  value = value.to_s if value.is_a?(Symbol)

  attribute_aliases = native_attribute_aliases.map{|a| a[:alias_attribute] }.compact_blank

  proc = attribute_aliases.find do |a| 
    a[:e_plat_key].to_s == e_plat_key.to_s && a[:custom_native_setter]
  end.try(:[], :custom_native_setter)

  eval(proc).call(value) if proc
end

#native_top_keyObject

The below methods are added to via the individual mapping classes



40
41
42
# File 'lib/e_plat/mapping/base.rb', line 40

def native_top_key
  :itself # or top key of JSON response e.g. "data", "product", 
end

#platformObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/e_plat/mapping/base.rb', line 115

def platform
  class_name = self.class.name.underscore
  case
  when class_name.include?("shopify")
    :shopify
  when class_name.include?("bigcommerce")
    :bigcommerce
  when class_name.include?("woocommerce")
    :woocommerce
  end
end

#protected_attributes_rename_on_initializeObject

for resources with keys in EPlat::Base::PROTECTED_ATTRIBUTES we can swap over before init and restore in as_json. => e_plat_key



67
# File 'lib/e_plat/mapping/base.rb', line 67

def protected_attributes_rename_on_initialize = {}

#to_e_plat_keys(attributes) ⇒ Object

e.g. “A great t-shirt” => “A great t-shirt”



102
103
104
105
# File 'lib/e_plat/mapping/base.rb', line 102

def to_e_plat_keys(attributes) #e.g. {description: "A great t-shirt"} => {body_html: "A great t-shirt"}
  as = aliases
  attributes.with_indifferent_access.transform_keys {|key| (as[key]) ? as[key] : key }
end

#to_native_keys(attributes) ⇒ Object

e.g. “A great t-shirt” => “A great t-shirt”



97
98
99
100
# File 'lib/e_plat/mapping/base.rb', line 97

def to_native_keys(attributes) #e.g. {body_html: "A great t-shirt"} => {description: "A great t-shirt"}
  as = aliases.invert
  attributes.with_indifferent_access.transform_keys{|key| (as[key]) ? as[key] : key }
end

#via_native_attributes_where_possible(params, search: false) ⇒ Object

e.g. ‘paid’ => 11



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/e_plat/mapping/base.rb', line 127

def via_native_attributes_where_possible(params, search:false) # e.g. {financial_status: 'paid'} => {status_id: 11}
  e_plat_key_mappings = keys_aliased_to_native_keys.with_indifferent_access # {e_plat_key: native_key}
  translated_attributes = {}

  params.each do |e_plat_key, value|
    native_key = e_plat_key_mappings[e_plat_key]

    if native_key
      translated_attributes[native_key] = native_setter(e_plat_key.to_s, value) || value
    else
      translated_attributes[e_plat_key] = value
    end
  end

  if search && platform == :woocommerce
    translated_attributes.transform_keys!{(_1 == "name" || _1 == "sku") ? "search" : _1}
  end

  translated_attributes
end