Module: ShopifyTransporter::Shopify::AttributesHelpers

Included in:
AttributesAccumulator, Order, Product
Defined in:
lib/shopify_transporter/shopify/attributes_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_prefix(prefix, *attributes) ⇒ Object



10
11
12
# File 'lib/shopify_transporter/shopify/attributes_helpers.rb', line 10

def add_prefix(prefix, *attributes)
  [*attributes].map { |attribute| "#{prefix}#{attribute}".to_sym }
end

#attributes_present?(row, *attributes) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/shopify_transporter/shopify/attributes_helpers.rb', line 6

def attributes_present?(row, *attributes)
  row.slice(*attributes).any? { |_, v| v.present? }
end

#delete_empty_attributes(row) ⇒ Object



18
19
20
# File 'lib/shopify_transporter/shopify/attributes_helpers.rb', line 18

def delete_empty_attributes(row)
  row.delete_if { |_, v| v.respond_to?(:empty?) && v.empty? }
end

#drop_prefix(row, prefix) ⇒ Object



14
15
16
# File 'lib/shopify_transporter/shopify/attributes_helpers.rb', line 14

def drop_prefix(row, prefix)
  row.transform_keys { |key| key.to_s.remove(prefix).to_sym }
end

#map_from_key_to_val(mapper, input) ⇒ Object



37
38
39
40
41
# File 'lib/shopify_transporter/shopify/attributes_helpers.rb', line 37

def map_from_key_to_val(mapper, input)
  mapper.each_with_object({}) do |(key, value), attributes|
    attributes[value] = input[key] if input[key]
  end
end

#normalize_keys(row) ⇒ Object



22
23
24
# File 'lib/shopify_transporter/shopify/attributes_helpers.rb', line 22

def normalize_keys(row)
  row.compact.transform_keys { |key| normalize_string(key).to_sym }
end

#normalize_string(str) ⇒ Object



26
27
28
# File 'lib/shopify_transporter/shopify/attributes_helpers.rb', line 26

def normalize_string(str)
  str.to_s.parameterize.underscore
end

#rename_fields(row, field_map) ⇒ Object



30
31
32
33
34
35
# File 'lib/shopify_transporter/shopify/attributes_helpers.rb', line 30

def rename_fields(row, field_map)
  return row unless field_map.present?
  row = row.dup
  field_map.each { |from, to| row[to] = row.delete from if row[from].present? }
  row
end

#shopify_metafield_hash(key:, value:, value_type: 'string', namespace:) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/shopify_transporter/shopify/attributes_helpers.rb', line 43

def shopify_metafield_hash(key:, value:, value_type: 'string', namespace:)
  {
    'key' => key,
    'value' => value,
    'value_type' => value_type,
    'namespace' => namespace,
  }
end