Class: LedgerSync::Deserializer

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

Direct Known Subclasses

Ledgers::TestLedger::Deserializer

Defined Under Namespace

Classes: Delegator

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serialization::Mixin

included

Class Method Details

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



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ledger_sync/deserializer.rb', line 53

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

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

.attribute_classObject



66
67
68
# File 'lib/ledger_sync/deserializer.rb', line 66

def self.attribute_class
  Serialization::DeserializerAttribute
end

.attributesObject



70
71
72
# File 'lib/ledger_sync/deserializer.rb', line 70

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

.deserializer_from(resource_attribute, args = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/ledger_sync/deserializer.rb', line 98

def self.deserializer_from(resource_attribute, args = {})
  if args.key?(:deserializer)
    args.fetch(:deserializer)
  else
    resource_key = inferred_resource_class.resource_attributes[resource_attribute].type.resource_class.resource_type
    require "#{inferred_config.root_path}/#{resource_key}/deserializer"
    inferred_config.client_class.resources[resource_key]::Deserializer
  end
end

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



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

def self.references_many(resource_attribute, args = {}, &block)
  attribute(
    resource_attribute,
    {
      type: Serialization::Type::DeserializerReferencesManyType.new(
        deserializer: deserializer_from(resource_attribute, args)
      )
    }.merge(args),
    &block
  )
end

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



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

def self.references_one(resource_attribute, args = {}, &block)
  attribute(
    resource_attribute,
    {
      type: Serialization::Type::DeserializerReferencesOneType.new(
        deserializer: deserializer_from(resource_attribute, args)
      )
    }.merge(args),
    &block
  )
end

Instance Method Details

#attribute_value_from_ledger(hash:, deserializer_attribute:, resource:) ⇒ Object



23
24
25
26
27
28
# File 'lib/ledger_sync/deserializer.rb', line 23

def attribute_value_from_ledger(hash:, deserializer_attribute:, resource:)
  deserializer_attribute.value_from_hash(
    hash: hash,
    resource: resource
  )
end

#deserialize(args = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ledger_sync/deserializer.rb', line 30

def deserialize(args = {})
  hash     = args.fetch(:hash)
  resource = args.fetch(:resource)

  deserialize_into = resource.dup # Do not overwrite values in the resource
  hash = Util::HashHelpers.deep_stringify_keys(hash)

  self.class.attributes.each_value do |deserializer_attribute|
    value = attribute_value_from_ledger(
      hash: hash,
      deserializer_attribute: deserializer_attribute,
      resource: deserialize_into
    )

    deserialize_into.assign_attribute(
      deserializer_attribute.resource_attribute_dot_parts.first,
      value
    )
  end

  deserialize_into
end