Class: LedgerSync::Adaptors::LedgerSerializerAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/ledger_sync/adaptors/ledger_serializer_attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block: nil, id: false, ledger_attribute:, resource_attribute: nil, resource_class: nil, serializer: nil, type: LedgerSerializerType::ValueType) ⇒ LedgerSerializerAttribute

Returns a new instance of LedgerSerializerAttribute.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 17

def initialize(block: nil, id: false, ledger_attribute:, resource_attribute: nil, resource_class: nil, serializer: nil, type: LedgerSerializerType::ValueType)
  raise 'block and resource_attribute cannot both be present' unless block.nil? || resource_attribute.nil?

  @block = block
  @id = id
  @ledger_attribute = ledger_attribute.to_s
  @resource_attribute = resource_attribute.to_s
  @resource_class = resource_class
  @serializer = serializer
  @type = type.new
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



9
10
11
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 9

def block
  @block
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 9

def id
  @id
end

#ledger_attributeObject (readonly)

Returns the value of attribute ledger_attribute.



9
10
11
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 9

def ledger_attribute
  @ledger_attribute
end

#resource_attributeObject (readonly)

Returns the value of attribute resource_attribute.



9
10
11
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 9

def resource_attribute
  @resource_attribute
end

#resource_classObject (readonly)

Returns the value of attribute resource_class.



9
10
11
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 9

def resource_class
  @resource_class
end

#serializerObject (readonly)

Returns the value of attribute serializer.



9
10
11
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 9

def serializer
  @serializer
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 9

def type
  @type
end

Instance Method Details

#block_value_for(resource:) ⇒ Object



29
30
31
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 29

def block_value_for(resource:)
  block.call(resource)
end

#dot_value_for(resource:) ⇒ Object

Make nested/dot calls (e.g. ‘vendor.ledger_id`)



34
35
36
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 34

def dot_value_for(resource:)
  resource_attribute_dot_parts.inject(resource) { |r, dot_method| r&.send(dot_method) }
end

#ledger_attribute_dot_partsObject



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

def ledger_attribute_dot_parts
  @ledger_attribute_dot_parts ||= ledger_attribute.split('.')
end

#ledger_attribute_hash_for(resource:) ⇒ Object



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

def ledger_attribute_hash_for(resource:)
  ledger_attribute_hash_with(value: value_from_local(resource: resource))
end

#ledger_attribute_hash_with(value:) ⇒ Object

Create nested hash for ledger

when ledger_attribute = ‘Foo.Bar.Baz’, ledger_attribute_hash_with(value: 123) Result: {

'Foo' => {
  'Bar' => {
    'Baz' => 123
  }
}

}



58
59
60
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 58

def ledger_attribute_hash_with(value:)
  ledger_attribute_dot_parts.reverse.inject(value) { |a, n| { n => a } }
end

#references_many?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 62

def references_many?
  type.is_a?(LedgerSerializerType::ReferencesManyType)
end

#resource_attribute?Boolean

Returns:

  • (Boolean)


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

def resource_attribute?
  resource_attribute.present?
end

#resource_attribute_dot_partsObject



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

def resource_attribute_dot_parts
  @resource_attribute_dot_parts ||= resource_attribute.split('.')
end

#value_from_ledger(hash:, resource:) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 93

def value_from_ledger(hash:, resource:)
  value = hash.dig(*ledger_attribute_dot_parts)

  value = if references_many?
            type.convert_from_ledger(
              resource_class: resource_class,
              serializer: serializer,
              value: value
            )
          else
            type.convert_from_ledger(
              value: value
            )
          end

  return value if resource_attribute_dot_parts.count <= 1

  nested_resource = resource.send(resource_attribute_dot_parts.first)
  nested_resource ||= resource_attribute_class(resource: resource).new

  build_resource_value_from_nested_attributes(
    nested_resource,
    value,
    resource_attribute_dot_parts[1..-1]
  )
end

#value_from_local(resource:) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 74

def value_from_local(resource:)
  value = if block.nil?
            dot_value_for(resource: resource)
          else
            block_value_for(resource: resource)
          end

  if references_many?
    type.convert_from_local(
      serializer: serializer,
      value: value
    )
  else
    type.convert_from_local(
      value: value
    )
  end
end