Class: LedgerSync::Domains::Serializer

Inherits:
Serializer
  • Object
show all
Defined in:
lib/ledger_sync/domains/serializer.rb,
lib/ledger_sync/domains/serializer/query.rb,
lib/ledger_sync/domains/serializer/struct.rb

Defined Under Namespace

Classes: Query, Struct

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.split_attributesObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ledger_sync/domains/serializer.rb', line 8

def self.split_attributes
  regular = []
  references = []

  attributes.each_value do |attr|
    if attr.references_many? || attr.references_one?
      references.push(attr)
    else
      regular.push(attr)
    end
  end
  [regular, references]
end

Instance Method Details

#serialize(args = {}) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ledger_sync/domains/serializer.rb', line 22

def serialize(args = {}) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  only_changes = args.fetch(:only_changes, false)
  resource     = args.fetch(:resource)

  ret = {}

  regular, references = self.class.split_attributes
  regular.each do |serializer_attribute|
    if (only_changes && !resource.attribute_changed?(serializer_attribute.resource_attribute)) || # rubocop:disable Layout/LineLength
       (serializer_attribute.if_method.present? && !send(serializer_attribute.if_method, resource: resource)) # rubocop:disable Layout/LineLength
      next
    end

    ret = LedgerSync::Util::HashHelpers.deep_merge(
      hash_to_merge_into: ret,
      other_hash: serializer_attribute.hash_attribute_hash_for(resource: resource)
    )
  end
  Serializer::Struct.build(
    ret, self.class.to_s,
    resource: resource, references: references
  )
end