Class: LedgerSync::QuickBooksOnline::Deserializer

Inherits:
Deserializer
  • Object
show all
Defined in:
lib/ledger_sync/quickbooks_online/deserializer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.amount(resource_attribute, args = {}) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/ledger_sync/quickbooks_online/deserializer.rb', line 74

def self.amount(resource_attribute, args = {})
  attribute(
    resource_attribute,
    {
      type: Serialization::Type::AmountFloatToIntegerType.new
    }.merge(args)
  )
end

.date(resource_attribute, args = {}) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/ledger_sync/quickbooks_online/deserializer.rb', line 83

def self.date(resource_attribute, args = {})
  attribute(
    resource_attribute,
    {
      type: LedgerSync::Serialization::Type::ParseDateType.new(format: '%Y-%m-%d')
    }.merge(args)
  )
end

.idObject



92
93
94
# File 'lib/ledger_sync/quickbooks_online/deserializer.rb', line 92

def self.id
  attribute(:ledger_id, hash_attribute: 'Id')
end

Instance Method Details

#deserialize(args = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ledger_sync/quickbooks_online/deserializer.rb', line 8

def deserialize(args = {})
  hash                  = args.fetch(:hash)
  merge_for_full_update = args.fetch(:merge_for_full_update, false)
  resource              = args.fetch(:resource)

  deserialized_resource = super(hash: hash, resource: resource)

  # Ref: https://github.com/LedgerSync/ledger_sync/issues/86
  if deserialized_resource.is_a?(LedgerSync::QuickBooksOnline::Account) &&
     deserialized_resource.AccountType &&
     deserialized_resource.Classification.blank?
    deserialized_resource.Classification = deserialized_resource.class::TYPES_TO_CLASSIFICATION_MAPPING.fetch(
      deserialized_resource.AccountType,
      nil
    )
  end

  return deserialized_resource unless merge_for_full_update

  merge_resources_for_full_update(
    hash: hash,
    ledger_resource: deserialized_resource,
    resource: resource
  )
end

#merge_resources_for_full_update(args = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ledger_sync/quickbooks_online/deserializer.rb', line 34

def merge_resources_for_full_update(args = {})
  hash            = args.fetch(:hash)
  resource        = args.fetch(:resource)
  merged_resource = resource.dup

  self.class.attributes.each do |_key, deserializer_attribute|
    next unless deserializer_attribute.references_many?

    resources_from_ledger = attribute_value_from_ledger(
      hash: hash,
      deserializer_attribute: deserializer_attribute,
      resource: merged_resource
    )

    resource_hash_from_ledger = resources_from_ledger.map { |e| [e.ledger_id, e] }.to_h

    # Using original resource since it is overwritten by keyword arg
    merged_value = resource.send(deserializer_attribute.resource_attribute_dot_parts.first).map do |referenced|
      if referenced.ledger_id.nil? # Assume creating a new resource in ledger
        referenced
      elsif resource_hash_from_ledger.key?(referenced.ledger_id.to_s) # Merged ledger into local
        ledger_resource = resource_hash_from_ledger[referenced.ledger_id.to_s]
        ledger_resource_serializer = deserializer_attribute.type.deserializer.inferred_serializer_class.new
        deserializer_attribute.type.deserializer.new.deserialize(
          hash: ledger_resource_serializer.serialize(resource: ledger_resource),
          resource: referenced
        )
      end
      # Else assume delete from ledger
    end.compact

    merged_resource.assign_attribute(
      deserializer_attribute.resource_attribute_dot_parts.first,
      merged_value
    )
  end

  merged_resource
end