Class: LedgerSync::Util::ResourceConverter
- Inherits:
-
Object
- Object
- LedgerSync::Util::ResourceConverter
show all
- Defined in:
- lib/ledger_sync/util/resource_converter.rb,
lib/ledger_sync/util/resource_converter/attribute.rb,
lib/ledger_sync/util/resource_converter/attribute_set.rb,
lib/ledger_sync/util/resource_converter/type/references_one_type.rb,
lib/ledger_sync/util/resource_converter/type/references_many_type.rb,
lib/ledger_sync/util/resource_converter/type/resource_converter_type.rb
Defined Under Namespace
Modules: Type
Classes: Attribute, AttributeSet
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
._attribute(destination_attribute = nil, args = {}, &block) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 42
def self._attribute(destination_attribute = nil, args = {}, &block)
if args.key?(:destination_attribute)
raise 'You cannot provide destination_attribute in args. Pass the value as the first argument.'
end
attributes.add(
ResourceConverter::Attribute.new(
args.merge(
block: (block if block_given?),
destination_attribute: destination_attribute
)
)
)
end
|
._references(destination_attribute = nil, args = {}) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 65
def self._references(destination_attribute = nil, args = {}, &)
reference_type = args.fetch(:reference_type)
resource_converter = args.fetch(:resource_converter)
reference_type_class = case reference_type
when :many
ResourceConverter::Type::ReferencesManyType
when :one
ResourceConverter::Type::ReferencesOneType
else
raise "Unkown reference type: #{references_type}"
end
_attribute(
destination_attribute,
{
type: reference_type_class.new(
resource_converter: resource_converter
)
}.merge(
args.except(
i[
reference_type
resource_converter
]
)
),
&
)
end
|
.attribute ⇒ Object
57
58
59
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 57
def self.attribute(...)
_attribute(...)
end
|
.attributes ⇒ Object
61
62
63
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 61
def self.attributes
@attributes ||= ResourceConverter::AttributeSet.new(resource_converter_class: self)
end
|
.references_many(destination_attribute = nil, args = {}) ⇒ Object
106
107
108
109
110
111
112
113
114
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 106
def self.references_many(destination_attribute = nil, args = {}, &)
_references(
destination_attribute,
{
reference_type: :many
}.merge(args),
&
)
end
|
.references_one(destination_attribute = nil, args = {}) ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 96
def self.references_one(destination_attribute = nil, args = {}, &)
_references(
destination_attribute,
{
reference_type: :one
}.merge(args),
&
)
end
|
Instance Method Details
#convert(args = {}) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 10
def convert(args = {})
destination = args.fetch(:destination).dup
destination = Util::HashHelpers.deep_stringify_keys(destination) if destination.is_a?(Hash)
only_changes = args.fetch(:only_changes, false)
source = args.fetch(:source).dup
source = Util::HashHelpers.deep_stringify_keys(source) if source.is_a?(Hash)
convertable_attributes(only_changes: only_changes, source: source).each do |converter_attribute|
args_to_pass = {
destination: destination,
source: source
}
next if converter_attribute.if_method.present? && !send(converter_attribute.if_method, args_to_pass)
destination = converter_attribute.build_destination!(args_to_pass)
end
destination
end
|
#convertable_attributes(args = {}) ⇒ Object
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 31
def convertable_attributes(args = {})
converter_attributes = self.class.attributes
source = args.fetch(:source)
return converter_attributes unless args[:only_changes] == true
raise 'only_changes can only be passed when the source is a resource' if source.is_a?(Hash)
converter_attributes.select { |e| source.attribute_changed?(e.source_attribute) }
end
|