Class: LedgerSync::Serializer

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

Direct Known Subclasses

Ledgers::TestLedger::Serializer

Defined Under Namespace

Classes: Delegator

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LedgerSync::Serialization::Mixin

included

Class Method Details

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



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ledger_sync/serializer.rb', line 42

def self.attribute(hash_attribute, args = {}, &block)
  if args.key?(:hash_attribute)
    raise 'You cannot provide hash_attribute in args.  Pass the value as the first argument.'
  end

  _attribute(
    args.merge(
      block: (block if block_given?),
      hash_attribute: hash_attribute
    )
  )
end

.attribute_classObject



55
56
57
# File 'lib/ledger_sync/serializer.rb', line 55

def self.attribute_class
  Serialization::SerializerAttribute
end

.attributesObject



59
60
61
# File 'lib/ledger_sync/serializer.rb', line 59

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

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



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ledger_sync/serializer.rb', line 75

def self.references_many(hash_attribute, args = {}, &block)
  attribute(
    hash_attribute,
    {
      type: Serialization::Type::SerializerReferencesManyType.new(
        serializer: serializer_from(hash_attribute, args)
      )
    }.merge(args),
    &block
  )
end

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



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ledger_sync/serializer.rb', line 63

def self.references_one(hash_attribute, args = {}, &block)
  attribute(
    hash_attribute,
    {
      type: Serialization::Type::SerializerReferencesOneType.new(
        serializer: serializer_from(hash_attribute, args)
      )
    }.merge(args),
    &block
  )
end

.serializer_from(hash_attribute, args = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/ledger_sync/serializer.rb', line 87

def self.serializer_from(hash_attribute, args = {})
  if args.key?(:serializer)
    args.fetch(:serializer)
  else
    resource_key = inferred_resource_class.resource_attributes[hash_attribute].type.resource_class.resource_type
    require File.join(inferred_config.root_path, resource_key.to_s, 'serializer')
    inferred_config.client_class.resources[resource_key]::Serializer
  end
end

Instance Method Details

#serialize(args = {}) ⇒ Object



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

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)
    next if serializer_attribute.if_method.present? && !send(serializer_attribute.if_method, resource: resource)

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

  ret
end