Class: LedgerSync::Serializer

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

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
# File 'lib/ledger_sync/serializer.rb', line 42

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

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

.attribute_classObject



53
54
55
# File 'lib/ledger_sync/serializer.rb', line 53

def self.attribute_class
  Serialization::SerializerAttribute
end

.attributesObject



57
58
59
# File 'lib/ledger_sync/serializer.rb', line 57

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

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



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

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



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

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



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

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 "ledger_sync/ledgers/#{inferred_client_class.root_key}/#{resource_key}/serializer"
    inferred_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