Class: LedgerSync::Adaptors::LedgerSerializer
- Inherits:
-
Object
- Object
- LedgerSync::Adaptors::LedgerSerializer
show all
- Defined in:
- lib/ledger_sync/adaptors/ledger_serializer.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of LedgerSerializer.
13
14
15
16
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 13
def initialize(resource:)
@resource = resource
ensure_inferred_resource_class!
end
|
Instance Attribute Details
#resource ⇒ Object
Returns the value of attribute resource.
11
12
13
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 11
def resource
@resource
end
|
Class Method Details
._inferred_resource_class ⇒ Object
93
94
95
96
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 93
def self._inferred_resource_class
parts = name.split('::')
LedgerSync.const_get(parts[parts.index('Adaptors') + 2])
end
|
.attribute(ledger_attribute:, resource_attribute: nil, type: LedgerSerializerType::ValueType, &block) ⇒ Object
62
63
64
65
66
67
68
69
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 62
def self.attribute(ledger_attribute:, resource_attribute: nil, type: LedgerSerializerType::ValueType, &block)
_attribute(
block: (block if block_given?),
ledger_attribute: ledger_attribute,
resource_attribute: resource_attribute,
type: type
)
end
|
.attributes ⇒ Object
71
72
73
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 71
def self.attributes
@attributes ||= LedgerSerializerAttributeSet.new(serializer_class: self)
end
|
.id(ledger_attribute:, resource_attribute:, &block) ⇒ Object
84
85
86
87
88
89
90
91
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 84
def self.id(ledger_attribute:, resource_attribute:, &block)
@id ||= _attribute(
block: (block if block_given?),
id: true,
ledger_attribute: ledger_attribute,
resource_attribute: resource_attribute
)
end
|
.references_many(ledger_attribute:, resource_attribute:, serializer:) ⇒ Object
75
76
77
78
79
80
81
82
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 75
def self.references_many(ledger_attribute:, resource_attribute:, serializer:)
_attribute(
ledger_attribute: ledger_attribute,
resource_attribute: resource_attribute,
serializer: serializer,
type: LedgerSerializerType::ReferencesManyType
)
end
|
Instance Method Details
#attribute_value_from_ledger(hash:, ledger_serializer_attribute:, resource:) ⇒ Object
18
19
20
21
22
23
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 18
def attribute_value_from_ledger(hash:, ledger_serializer_attribute:, resource:)
ledger_serializer_attribute.value_from_ledger(
hash: hash,
resource: resource
)
end
|
#deserialize(hash:) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 25
def deserialize(hash:)
deserialize_into = resource.dup hash = Util::HashHelpers.deep_stringify_keys(hash)
self.class.attributes.each do |ledger_serializer_attribute|
next unless ledger_serializer_attribute.resource_attribute?
value = attribute_value_from_ledger(
hash: hash,
ledger_serializer_attribute: ledger_serializer_attribute,
resource: deserialize_into
)
deserialize_into.assign_attribute(
ledger_serializer_attribute.resource_attribute_dot_parts.first,
value
)
end
deserialize_into
end
|
#to_ledger_hash(only_changes: false) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 47
def to_ledger_hash(only_changes: false)
ret = {}
self.class.attributes.each do |ledger_serializer_attribute|
next if only_changes && !resource.attribute_changed?(ledger_serializer_attribute.resource_attribute)
ret = Util::HashHelpers.deep_merge(
hash_to_merge_into: ret,
other_hash: ledger_serializer_attribute.ledger_attribute_hash_for(resource: resource)
)
end
ret
end
|