Class: LedgerSync::Adaptors::LedgerSerializerAttribute
- Inherits:
-
Object
- Object
- LedgerSync::Adaptors::LedgerSerializerAttribute
- Defined in:
- lib/ledger_sync/adaptors/ledger_serializer_attribute.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#ledger_attribute ⇒ Object
readonly
Returns the value of attribute ledger_attribute.
-
#resource_attribute ⇒ Object
readonly
Returns the value of attribute resource_attribute.
-
#resource_class ⇒ Object
readonly
Returns the value of attribute resource_class.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #block_value_for(resource:) ⇒ Object
-
#dot_value_for(resource:) ⇒ Object
Make nested/dot calls (e.g. ‘vendor.ledger_id`).
-
#initialize(block: nil, id: false, ledger_attribute:, resource_attribute: nil, resource_class: nil, serializer: nil, type: LedgerSerializerType::ValueType) ⇒ LedgerSerializerAttribute
constructor
A new instance of LedgerSerializerAttribute.
- #ledger_attribute_dot_parts ⇒ Object
- #ledger_attribute_hash_for(resource:) ⇒ Object
-
#ledger_attribute_hash_with(value:) ⇒ Object
Create nested hash for ledger.
- #references_many? ⇒ Boolean
- #resource_attribute? ⇒ Boolean
- #resource_attribute_dot_parts ⇒ Object
- #value_from_ledger(hash:, resource:) ⇒ Object
- #value_from_local(resource:) ⇒ Object
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
#block ⇒ Object (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 |
#id ⇒ Object (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_attribute ⇒ Object (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_attribute ⇒ Object (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_class ⇒ Object (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 |
#serializer ⇒ Object (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 |
#type ⇒ Object (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_parts ⇒ Object
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
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
66 67 68 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 66 def resource_attribute? resource_attribute.present? end |
#resource_attribute_dot_parts ⇒ Object
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 |