Class: EPlat::Mapping::Base
- Inherits:
-
Object
- Object
- EPlat::Mapping::Base
- 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
Constant Summary collapse
- DEFAULT_VALUES =
“head”, auto_uninstall: true, …
{}
Class Attribute Summary collapse
-
.virtual_collections ⇒ Object
Returns the value of attribute virtual_collections.
Instance Attribute Summary collapse
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#virtual_collection ⇒ Object
Returns the value of attribute virtual_collection.
Class Method Summary collapse
-
.virtual_collection(name, **options) ⇒ Object
virtual_collection :line_items, class: EPlat::Mapping::VirtualCollection::Bigcommerce::OrderLineItems, map_from: “consignments”.
Instance Method Summary collapse
-
#aliases ⇒ Object
The below methods add support for getting/setting/converting via e_plat and native keys.
-
#e_plat_key_to(native_key) ⇒ Object
e.g.
-
#initialize(resource_instance) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #keys_aliased_to_native_keys ⇒ Object
- #mappable_keys ⇒ Object
-
#mappings ⇒ Object
e.g.
- #native_attribute_aliases ⇒ Object
- #native_attributes ⇒ Object
-
#native_key_to(e_plat_key) ⇒ Object
e.g.
-
#native_setter(e_plat_key, value) ⇒ Object
str, proc.call(value) | nil.
-
#native_top_key ⇒ Object
The below three methods are added to via the individual mapping classes.
-
#to_e_plat_keys(attributes) ⇒ Object
e.g.
-
#to_native_keys(attributes) ⇒ Object
e.g.
-
#via_native_attributes_where_possible(params) ⇒ Object
e.g.
Methods included from RequestBodyRoot
#include_root_in_request_body?
Constructor Details
#initialize(resource_instance) ⇒ Base
Returns a new instance of Base.
29 30 31 32 33 |
# File 'lib/e_plat/mapping/base.rb', line 29 def initialize(resource_instance) @resource = resource_instance init_virtual_collections if @resource.present? end |
Class Attribute Details
.virtual_collections ⇒ Object
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
#resource ⇒ Object
Returns the value of attribute resource.
5 6 7 |
# File 'lib/e_plat/mapping/base.rb', line 5 def resource @resource end |
#virtual_collection ⇒ Object
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, **) self.virtual_collections ||= [] self.virtual_collections.push({ name: name, class: [:class], map_from: [:map_from] }) define_method("init_#{name}") do |resource:, map_from:| instance_variable_set "@#{name}", "#{ [:class] }".constantize.new(resource: resource, map_from: map_from) end end |
Instance Method Details
#aliases ⇒ Object
The below methods add support for getting/setting/converting via e_plat and native keys
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/e_plat/mapping/base.rb', line 68 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”
90 91 92 |
# File 'lib/e_plat/mapping/base.rb', line 90 def e_plat_key_to(native_key) #e.g. "body_html" => "description" aliases[native_key] end |
#inspect ⇒ Object
35 36 37 |
# File 'lib/e_plat/mapping/base.rb', line 35 def inspect "#<#{self.class} #{ self.instance_variable_names.join(' ') }>" end |
#keys_aliased_to_native_keys ⇒ Object
82 83 84 |
# File 'lib/e_plat/mapping/base.rb', line 82 def keys_aliased_to_native_keys @e_plat_to_native_aliases ||= aliases.invert end |
#mappable_keys ⇒ Object
104 105 106 |
# File 'lib/e_plat/mapping/base.rb', line 104 def mappable_keys native_attribute_aliases.map{|a| a.values.first[:e_plat_key]}.presence || :all end |
#mappings ⇒ Object
e.g. native_key, ‘body_html’ => ‘description’
108 109 110 |
# File 'lib/e_plat/mapping/base.rb', line 108 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_aliases ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/e_plat/mapping/base.rb', line 49 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_attributes ⇒ Object
45 46 47 |
# File 'lib/e_plat/mapping/base.rb', line 45 def native_attributes [] end |
#native_key_to(e_plat_key) ⇒ Object
e.g. “description” => “body_html”
86 87 88 |
# File 'lib/e_plat/mapping/base.rb', line 86 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
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/e_plat/mapping/base.rb', line 129 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_key ⇒ Object
The below three methods are added to via the individual mapping classes
41 42 43 |
# File 'lib/e_plat/mapping/base.rb', line 41 def native_top_key :itself # or top key of JSON response e.g. "data", "product", end |
#to_e_plat_keys(attributes) ⇒ Object
e.g. “A great t-shirt” => “A great t-shirt”
99 100 101 102 |
# File 'lib/e_plat/mapping/base.rb', line 99 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”
94 95 96 97 |
# File 'lib/e_plat/mapping/base.rb', line 94 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) ⇒ Object
e.g. ‘paid’ => 11
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/e_plat/mapping/base.rb', line 112 def via_native_attributes_where_possible(params) # 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 translated_attributes end |