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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 38
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 = {}, &block) ⇒ Object
61
62
63
64
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
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 61
def self._references(destination_attribute = nil, args = {}, &block)
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
]
)
),
&block
)
end
|
.attribute(*args, &block) ⇒ Object
53
54
55
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 53
def self.attribute(*args, &block)
_attribute(*args, &block)
end
|
.attributes ⇒ Object
57
58
59
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 57
def self.attributes
@attributes ||= ResourceConverter::AttributeSet.new(resource_converter_class: self)
end
|
.references_many(destination_attribute = nil, args = {}, &block) ⇒ Object
102
103
104
105
106
107
108
109
110
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 102
def self.references_many(destination_attribute = nil, args = {}, &block)
_references(
destination_attribute,
{
reference_type: :many
}.merge(args),
&block
)
end
|
.references_one(destination_attribute = nil, args = {}, &block) ⇒ Object
92
93
94
95
96
97
98
99
100
|
# File 'lib/ledger_sync/util/resource_converter.rb', line 92
def self.references_one(destination_attribute = nil, args = {}, &block)
_references(
destination_attribute,
{
reference_type: :one
}.merge(args),
&block
)
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
30
31
32
33
34
35
36
|
# 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)
if source.is_a?(Hash) && args.key?(:only_changes)
raise 'only_changes can only be passed when the source is a resource'
end
self.class.attributes.each do |converter_attribute|
next if only_changes && !source.attribute_changed?(converter_attribute.source_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
|