Class: LedgerSync::Adaptors::QuickBooksOnline::LedgerSerializer

Inherits:
LedgerSerializer
  • Object
show all
Defined in:
lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer.rb

Class Attribute Summary collapse

Attributes inherited from LedgerSerializer

#resource

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LedgerSerializer

attribute, #attribute_value_from_ledger, attributes, #initialize, references_many, references_one

Methods included from Mixins::InferResourceClassMixin

included

Constructor Details

This class inherits a constructor from LedgerSync::Adaptors::LedgerSerializer

Class Attribute Details

.quickbooks_online_resource_types_hashObject (readonly)

Returns the value of attribute quickbooks_online_resource_types_hash.



71
72
73
# File 'lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer.rb', line 71

def quickbooks_online_resource_types_hash
  @quickbooks_online_resource_types_hash
end

Class Method Details

.id(**keywords) ⇒ Object



74
75
76
# File 'lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer.rb', line 74

def self.id(**keywords)
  super({ ledger_attribute: 'Id', resource_attribute: :ledger_id }.merge(keywords))
end

.ledger_serializer_for(resource_class:) ⇒ Object



78
79
80
# File 'lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer.rb', line 78

def self.ledger_serializer_for(resource_class:)
  QuickBooksOnline.const_get("#{resource_class.name.split('LedgerSync::')[1..-1].join('LedgerSync::')}::LedgerSerializer")
end

.quickbooks_online_resource_typeObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer.rb', line 82

def self.quickbooks_online_resource_type
  @quickbooks_online_resource_type ||= begin
    qbo_type = Adaptor.ledger_resource_type_for(
      resource_class: inferred_resource_class
    )
    type_hash = QuickBooksOnline::LedgerSerializer
                .quickbooks_online_resource_types_hash
                .try(:fetch, qbo_type, nil)
    raise "Cannot define type in #{name}.  Type already exists: #{qbo_type}.  Defined previously by #{quickbooks_online_resource_types_hash[qbo_type][:serializer_class].name}" if type_hash.present? && (type_hash[:serializer_class] != serializer_class || type_hash[:resource_class] != inferred_resource_class)

    QuickBooksOnline::LedgerSerializer.class_eval do
      @quickbooks_online_resource_types_hash ||= {}
    end

    QuickBooksOnline::LedgerSerializer.quickbooks_online_resource_types_hash[qbo_type] = {
      resource_class: inferred_resource_class,
      serializer_class: self
    }

    qbo_type
  end
end

Instance Method Details

#deserialize(hash:, merge_for_full_update: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer.rb', line 11

def deserialize(hash:, merge_for_full_update: false)
  deserialized_resource = super(hash: hash)

  # Ref: https://github.com/LedgerSync/ledger_sync/issues/86
  deserialized_resource.classification ||= LedgerSerializerType::AccountType::TYPE_TO_CLASSIFICATION_MAPPING.fetch(deserialized_resource., nil) if deserialized_resource.is_a?(LedgerSync::Account) && deserialized_resource.

  return deserialized_resource unless merge_for_full_update

  merge_resources_for_full_update(
    hash: hash,
    ledger_resource: deserialized_resource
  )
end

#merge_resources_for_full_update(hash:, ledger_resource:) ⇒ Object



25
26
27
28
29
30
31
32
33
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
# File 'lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer.rb', line 25

def merge_resources_for_full_update(hash:, ledger_resource:)
  merged_resource = resource.dup

  self.class.attributes.deserializable_attributes.each do |ledger_serializer_attribute|
    next unless ledger_serializer_attribute.references_many?

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

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

    # Using original resource since it is overwritten by keyword arg
    merged_value = resource.send(ledger_serializer_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 = ledger_serializer_attribute.serializer.new(resource: ledger_resource)
        ledger_serializer_attribute.serializer.new(resource: referenced).deserialize(hash: ledger_resource_serializer.to_ledger_hash)
      end
      # Else assume delete from ledger
    end.compact

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

  merged_resource
end

#to_ledger_hash(deep_merge_unmapped_values: {}, only_changes: false) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer.rb', line 60

def to_ledger_hash(deep_merge_unmapped_values: {}, only_changes: false)
  ret = super(only_changes: only_changes)
  return ret unless deep_merge_unmapped_values.any?

  deep_merge_if_not_mapped(
    current_hash: ret,
    hash_to_search: deep_merge_unmapped_values
  )
end