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.
-
#deserialize ⇒ Object
readonly
Returns the value of attribute deserialize.
-
#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.
-
#serialize ⇒ Object
readonly
Returns the value of attribute serialize.
-
#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
- #deserialize? ⇒ Boolean
-
#dot_value_for(resource:) ⇒ Object
Make nested/dot calls (e.g. ‘vendor.ledger_id`).
-
#initialize(block: nil, deserialize: true, id: false, ledger_attribute:, resource_attribute: nil, resource_class: nil, serialize: true, 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.
- #reference? ⇒ Boolean
- #references_many? ⇒ Boolean
- #references_one? ⇒ Boolean
- #resource_attribute? ⇒ Boolean
- #resource_attribute_dot_parts ⇒ Object
- #serialize? ⇒ Boolean
- #value_from_ledger(hash:, resource:) ⇒ Object
- #value_from_local(resource:) ⇒ Object
Constructor Details
#initialize(block: nil, deserialize: true, id: false, ledger_attribute:, resource_attribute: nil, resource_class: nil, serialize: true, serializer: nil, type: LedgerSerializerType::ValueType) ⇒ LedgerSerializerAttribute
Returns a new instance of LedgerSerializerAttribute.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 19 def initialize( block: nil, deserialize: true, id: false, ledger_attribute:, resource_attribute: nil, resource_class: nil, serialize: true, serializer: nil, type: LedgerSerializerType::ValueType ) raise 'block and resource_attribute cannot both be present' unless block.nil? || resource_attribute.nil? @block = block @deserialize = deserialize @id = id @ledger_attribute = ledger_attribute.to_s @resource_attribute = resource_attribute.to_s @resource_class = resource_class @serialize = serialize @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 |
#deserialize ⇒ Object (readonly)
Returns the value of attribute deserialize.
9 10 11 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 9 def deserialize @deserialize 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 |
#serialize ⇒ Object (readonly)
Returns the value of attribute serialize.
9 10 11 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 9 def serialize @serialize 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
43 44 45 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 43 def block_value_for(resource:) block.call(resource) end |
#deserialize? ⇒ Boolean
47 48 49 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 47 def deserialize? deserialize end |
#dot_value_for(resource:) ⇒ Object
Make nested/dot calls (e.g. ‘vendor.ledger_id`)
52 53 54 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 52 def dot_value_for(resource:) resource_attribute_dot_parts.inject(resource) { |r, dot_method| r&.send(dot_method) } end |
#ledger_attribute_dot_parts ⇒ Object
56 57 58 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 56 def ledger_attribute_dot_parts @ledger_attribute_dot_parts ||= ledger_attribute.split('.') end |
#ledger_attribute_hash_for(resource:) ⇒ Object
60 61 62 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 60 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
}
}
}
76 77 78 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 76 def ledger_attribute_hash_with(value:) ledger_attribute_dot_parts.reverse.inject(value) { |a, n| { n => a } } end |
#reference? ⇒ Boolean
80 81 82 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 80 def reference? references_many? || references_one? end |
#references_many? ⇒ Boolean
84 85 86 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 84 def references_many? type.is_a?(LedgerSerializerType::ReferencesManyType) end |
#references_one? ⇒ Boolean
88 89 90 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 88 def references_one? type.is_a?(LedgerSerializerType::ReferencesOneType) end |
#resource_attribute? ⇒ Boolean
92 93 94 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 92 def resource_attribute? resource_attribute.present? end |
#resource_attribute_dot_parts ⇒ Object
96 97 98 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 96 def resource_attribute_dot_parts @resource_attribute_dot_parts ||= resource_attribute.split('.') end |
#serialize? ⇒ Boolean
100 101 102 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 100 def serialize? serialize end |
#value_from_ledger(hash:, resource:) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 123 def value_from_ledger(hash:, resource:) value = hash.dig(*ledger_attribute_dot_parts) value = if reference? 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
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ledger_sync/adaptors/ledger_serializer_attribute.rb', line 104 def value_from_local(resource:) value = if block.nil? dot_value_for(resource: resource) else block_value_for(resource: resource) end if reference? type.convert_from_local( serializer: serializer, value: value ) else type.convert_from_local( value: value ) end end |