Class: App::AddressSqlStorageConverter

Inherits:
Object
  • Object
show all
Defined in:
app/converters/address_sql_storage_converter.rb

Constant Summary collapse

ADDRESS_TYPE_MAP =
Hash.new { |h, k|
  raise ArgumentError, "Addressable type #{k.inspect} not known"
}.update(
  shipping: 'ShippingAddress',
  billing: 'BillingAddress'
).freeze
REVERSE_ADDRESS_TYPE_MAP =
Hash.new { |h, k|
  raise ArgumentError, "Addressable type #{k.inspect} not known"
}.update(
  Hash[ADDRESS_TYPE_MAP.to_a.map(&:reverse)]
).freeze

Instance Method Summary collapse

Instance Method Details

#attributes_list_to_storage(list) ⇒ Object



16
17
18
19
20
21
22
# File 'app/converters/address_sql_storage_converter.rb', line 16

def attributes_list_to_storage(list)
  list.dup.tap { |converted|
    if converted.delete(:account_id)
      converted << :addressable_id << :addressable_type
    end
  }
end

#from_storage(attrs) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/converters/address_sql_storage_converter.rb', line 24

def from_storage(attrs)
  attrs.dup.tap { |copy|
    copy[:type] = REVERSE_ADDRESS_TYPE_MAP[copy[:type]] if copy[:type]
    addressable_id, addressable_type = copy.delete(:addressable_id), copy.delete(:addressable_type)
    if addressable_id && addressable_type
      replacement_attr = case addressable_type
      when 'Account'
        :account_id
      else
        raise ORMivore::BadAttributesError, "Unknown addressable_type #{addressable_type.inspect}"
      end
      copy[replacement_attr] = addressable_id
    end
  }
end

#to_storage(attrs) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'app/converters/address_sql_storage_converter.rb', line 40

def to_storage(attrs)
  attrs.dup.tap { |copy|
    copy[:type] = ADDRESS_TYPE_MAP[copy[:type]] if copy[:type]
     = copy.delete(:account_id)
    if 
      copy[:addressable_id] = 
      copy[:addressable_type] = 'Account'
    end
  }
end