Class: LedgerSync::Adaptors::LedgerSerializer
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.attribute(deserialize: true, ledger_attribute:, resource_attribute: nil, serialize: true, type: LedgerSerializerType::ValueType, &block) ⇒ Object
-
.attributes ⇒ Object
-
.id(ledger_attribute:, resource_attribute:, &block) ⇒ Object
-
.references_many(ledger_attribute:, resource_attribute:, serializer:) ⇒ Object
-
.references_one(ledger_attribute:, resource_attribute:, serializer:) ⇒ Object
Instance Method Summary
collapse
included
Constructor Details
Returns a new instance of LedgerSerializer.
15
16
17
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 15
def initialize(**keywords)
@resource = keywords.fetch(:resource)
end
|
Instance Attribute Details
#resource ⇒ Object
Returns the value of attribute resource.
13
14
15
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 13
def resource
@resource
end
|
Class Method Details
.attribute(deserialize: true, ledger_attribute:, resource_attribute: nil, serialize: true, type: LedgerSerializerType::ValueType, &block) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 63
def self.attribute(
deserialize: true,
ledger_attribute:,
resource_attribute: nil,
serialize: true,
type: LedgerSerializerType::ValueType,
&block
)
_attribute(
block: (block if block_given?),
deserialize: deserialize,
ledger_attribute: ledger_attribute,
resource_attribute: resource_attribute,
serialize: serialize,
type: type
)
end
|
.attributes ⇒ Object
81
82
83
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 81
def self.attributes
@attributes ||= LedgerSerializerAttributeSet.new(serializer_class: self)
end
|
.id(ledger_attribute:, resource_attribute:, &block) ⇒ Object
103
104
105
106
107
108
109
110
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 103
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
85
86
87
88
89
90
91
92
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 85
def self.references_many(ledger_attribute:, resource_attribute:, serializer:)
_attribute(
ledger_attribute: ledger_attribute,
resource_attribute: resource_attribute,
serializer: serializer,
type: LedgerSerializerType::ReferencesManyType
)
end
|
.references_one(ledger_attribute:, resource_attribute:, serializer:) ⇒ Object
94
95
96
97
98
99
100
101
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 94
def self.references_one(ledger_attribute:, resource_attribute:, serializer:)
_attribute(
ledger_attribute: ledger_attribute,
resource_attribute: resource_attribute,
serializer: serializer,
type: LedgerSerializerType::ReferencesOneType
)
end
|
Instance Method Details
#attribute_value_from_ledger(hash:, ledger_serializer_attribute:, resource:) ⇒ Object
19
20
21
22
23
24
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 19
def attribute_value_from_ledger(hash:, ledger_serializer_attribute:, resource:)
ledger_serializer_attribute.value_from_ledger(
hash: hash,
resource: resource
)
end
|
#deserialize(hash:) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 26
def deserialize(hash:)
deserialize_into = resource.dup hash = Util::HashHelpers.deep_stringify_keys(hash)
self.class.attributes.deserializable_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/ledger_sync/adaptors/ledger_serializer.rb', line 48
def to_ledger_hash(only_changes: false)
ret = {}
self.class.attributes.serializable_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
|