Class: LedgerSync::Serializer

Inherits:
Object
  • Object
show all
Includes:
LedgerSync::Serialization::Mixin
Defined in:
lib/ledger_sync/serializer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LedgerSync::Serialization::Mixin

included

Class Method Details

.attribute(hash_attribute, args = {}, &block) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/ledger_sync/serializer.rb', line 29

def self.attribute(hash_attribute, args = {}, &block)
  _attribute(
    args.merge(
      block: (block if block_given?),
      hash_attribute: hash_attribute
    )
  )
end

.attribute_classObject



38
39
40
# File 'lib/ledger_sync/serializer.rb', line 38

def self.attribute_class
  Serialization::SerializerAttribute
end

.attributesObject



42
43
44
# File 'lib/ledger_sync/serializer.rb', line 42

def self.attributes
  @attributes ||= Serialization::SerializerAttributeSet.new(serializer_class: self)
end

Instance Method Details

#serialize(args = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ledger_sync/serializer.rb', line 11

def serialize(args = {})
  only_changes = args.fetch(:only_changes, false)
  resource     = args.fetch(:resource)

  ret = {}

  self.class.attributes.each_value do |serializer_attribute|
    next if only_changes && !resource.attribute_changed?(serializer_attribute.resource_attribute)

    ret = Util::HashHelpers.deep_merge(
      hash_to_merge_into: ret,
      other_hash: serializer_attribute.hash_attribute_hash_for(resource: resource)
    )
  end

  ret
end