yaoc 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -2
- data/examples/04_compositions.rb +3 -2
- data/examples/05_fill_existing_objects.rb +11 -0
- data/lib/yaoc.rb +6 -1
- data/lib/yaoc/helper/scope.rb +34 -0
- data/lib/yaoc/helper/thread_local_storage.rb +20 -0
- data/lib/yaoc/mapper_chain.rb +78 -0
- data/lib/yaoc/mapper_registry.rb +18 -0
- data/lib/yaoc/mapping_to_class.rb +1 -1
- data/lib/yaoc/object_mapper.rb +21 -4
- data/lib/yaoc/version.rb +1 -1
- data/spec/acceptance/map_multiple_objects_to_one_in_a_chain_spec.rb +78 -0
- data/spec/integration/lib/yaoc/helper/scope_spec.rb +14 -0
- data/spec/unit/lib/yaoc/helper/scope_spec.rb +57 -0
- data/spec/unit/lib/yaoc/helper/thread_local_storage_spec.rb +35 -0
- data/spec/unit/lib/yaoc/mapper_chain_spec.rb +154 -0
- data/spec/unit/lib/yaoc/mapper_registry_spec.rb +21 -0
- data/spec/unit/lib/yaoc/object_mapper_spec.rb +20 -0
- metadata +17 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64462398acb11cc3f3ef2ddb2c1076de8aaf1855
|
4
|
+
data.tar.gz: ffb691a062e66431fd84e6aea85b36f23fbd42d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71223debc913dd57538de730514f00fc645f40c05262ac271d411a39ac35a5c377d6f5a0b6f17b9286166a30a505d084f55b62b5fc7a2f8220680608b35b7e57
|
7
|
+
data.tar.gz: 46556a67045853315b84949f7193212558e8d6ff7d0f2e1587a6e2c5b2c2483d837636a12e201a2cfec4448dc3dcc91d168f78a201cdd6ca1c20f746e7d985c2
|
data/README.md
CHANGED
@@ -226,8 +226,9 @@ Role = StructHE(:id, :name)
|
|
226
226
|
OldRole = StructHE(:o_id, :o_name)
|
227
227
|
|
228
228
|
|
229
|
-
|
229
|
+
Yaoc::ObjectMapper.new(Role, OldRole).tap do |mapper|
|
230
230
|
mapper.add_mapping do
|
231
|
+
register_as :role_mapper
|
231
232
|
fetcher :public_send
|
232
233
|
|
233
234
|
rule to: :id, from: :o_id
|
@@ -245,7 +246,7 @@ user_mapper = Yaoc::ObjectMapper.new(User4, OldUser4).tap do |mapper|
|
|
245
246
|
|
246
247
|
rule to: :roles,
|
247
248
|
from: :o_roles,
|
248
|
-
object_converter: role_mapper,
|
249
|
+
object_converter: :role_mapper,
|
249
250
|
is_collection: true
|
250
251
|
|
251
252
|
end
|
@@ -304,10 +305,17 @@ end
|
|
304
305
|
|
305
306
|
old_role = RoleThing.new(id: 1, role: "my_role")
|
306
307
|
old_user5 = OldUser5.new(id: 1, name: "my fullname")
|
308
|
+
|
307
309
|
new_user5 = user_mapper.load(old_user5)
|
308
310
|
|
309
311
|
role_mapper.load(old_role, new_user5)
|
310
312
|
|
313
|
+
# OR
|
314
|
+
#
|
315
|
+
# mapper_chain = Yaoc::MapperChain.new(user_mapper, role_mapper)
|
316
|
+
# new_user5 = mapper_chain.load_all([old_user5, old_role])
|
317
|
+
|
318
|
+
|
311
319
|
puts old_user5
|
312
320
|
puts old_role
|
313
321
|
puts new_user5
|
data/examples/04_compositions.rb
CHANGED
@@ -19,8 +19,9 @@ Role = StructHE(:id, :name)
|
|
19
19
|
OldRole = StructHE(:o_id, :o_name)
|
20
20
|
|
21
21
|
|
22
|
-
|
22
|
+
Yaoc::ObjectMapper.new(Role, OldRole).tap do |mapper|
|
23
23
|
mapper.add_mapping do
|
24
|
+
register_as :role_mapper
|
24
25
|
fetcher :public_send
|
25
26
|
|
26
27
|
rule to: :id, from: :o_id
|
@@ -38,7 +39,7 @@ user_mapper = Yaoc::ObjectMapper.new(User4, OldUser4).tap do |mapper|
|
|
38
39
|
|
39
40
|
rule to: :roles,
|
40
41
|
from: :o_roles,
|
41
|
-
object_converter: role_mapper,
|
42
|
+
object_converter: :role_mapper,
|
42
43
|
is_collection: true
|
43
44
|
|
44
45
|
end
|
@@ -38,8 +38,19 @@ puts old_user5
|
|
38
38
|
puts old_role
|
39
39
|
puts new_user5
|
40
40
|
|
41
|
+
puts "\n" * 5
|
42
|
+
|
43
|
+
# OR
|
44
|
+
|
45
|
+
mapper_chain = Yaoc::MapperChain.new(user_mapper, role_mapper)
|
46
|
+
|
47
|
+
new_user5 = mapper_chain.load_all([old_user5, old_role])
|
41
48
|
|
42
49
|
|
50
|
+
puts old_user5
|
51
|
+
puts old_role
|
52
|
+
puts new_user5
|
53
|
+
|
43
54
|
puts "\n" * 5
|
44
55
|
|
45
56
|
|
data/lib/yaoc.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
require "yaoc/version"
|
2
2
|
|
3
|
-
|
3
|
+
require 'yaoc/helper/struct_hash_constructor'
|
4
|
+
require 'yaoc/helper/to_proc_delegator'
|
5
|
+
require 'yaoc/helper/thread_local_storage'
|
6
|
+
require 'yaoc/helper/scope'
|
4
7
|
|
8
|
+
require 'yaoc/mapper_registry'
|
5
9
|
require 'yaoc/mapping_base'
|
6
10
|
require 'yaoc/transformation_command'
|
7
11
|
require 'yaoc/transformation_deferred_command'
|
@@ -11,6 +15,7 @@ Dir[File.join(File.expand_path(__dir__ ), "yaoc/strategies/*.rb")].each { |f| re
|
|
11
15
|
|
12
16
|
require 'yaoc/converter_builder'
|
13
17
|
require 'yaoc/object_mapper'
|
18
|
+
require 'yaoc/mapper_chain'
|
14
19
|
|
15
20
|
module Yaoc
|
16
21
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Yaoc
|
2
|
+
module Helper
|
3
|
+
class Scope
|
4
|
+
|
5
|
+
attr_accessor :scope_name, :storage_source
|
6
|
+
|
7
|
+
def initialize(scope_name="default", storage_source=ThreadLocalStorage)
|
8
|
+
self.scope_name = scope_name
|
9
|
+
self.storage_source = storage_source
|
10
|
+
end
|
11
|
+
|
12
|
+
def storage
|
13
|
+
storage_source.for(scope_name).data
|
14
|
+
end
|
15
|
+
|
16
|
+
def []=(key, value)
|
17
|
+
self.storage[key]=value
|
18
|
+
end
|
19
|
+
|
20
|
+
def [](key)
|
21
|
+
self.storage[key]
|
22
|
+
end
|
23
|
+
|
24
|
+
def clear!
|
25
|
+
self.storage.clear
|
26
|
+
end
|
27
|
+
|
28
|
+
def fetch(*args, &block)
|
29
|
+
self.storage.fetch(*args, &block)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Yaoc
|
2
|
+
module Helper
|
3
|
+
class ThreadLocalStorage
|
4
|
+
class << self
|
5
|
+
private :new
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_accessor :data
|
9
|
+
|
10
|
+
def self.for(scope_name="default")
|
11
|
+
Thread.current["_#{name}_#{scope_name}"] ||= new
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
self.data||={}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Yaoc
|
2
|
+
class MapperChain
|
3
|
+
attr_accessor :converter, :last_result, :next_result
|
4
|
+
|
5
|
+
def self.registry
|
6
|
+
Yaoc::MapperRegistry
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(*converter)
|
10
|
+
self.converter = converter
|
11
|
+
end
|
12
|
+
|
13
|
+
def load_first(input_object, object_to_fill=nil)
|
14
|
+
converter_iterator.rewind
|
15
|
+
self.next_result = converter_iterator.next.load(input_object, object_to_fill)
|
16
|
+
end
|
17
|
+
|
18
|
+
def load_next(input_object)
|
19
|
+
self.next_result = converter_iterator.next.load(input_object, self.next_result)
|
20
|
+
rescue StopIteration
|
21
|
+
raise "ToManyInputObjects"
|
22
|
+
end
|
23
|
+
|
24
|
+
def load_all(input_objects, object_to_fill=nil)
|
25
|
+
each_object_with_converter(input_objects) do |converter, input_object|
|
26
|
+
object_to_fill = converter.load(input_object, object_to_fill)
|
27
|
+
end
|
28
|
+
|
29
|
+
self.last_result = object_to_fill
|
30
|
+
end
|
31
|
+
|
32
|
+
def dump_first(input_object, object_to_fill=nil)
|
33
|
+
converter_iterator.rewind
|
34
|
+
self.next_result = converter_iterator.next.dump(input_object, object_to_fill)
|
35
|
+
end
|
36
|
+
|
37
|
+
def dump_next(input_object)
|
38
|
+
self.next_result = converter_iterator.next.dump(input_object, self.next_result)
|
39
|
+
end
|
40
|
+
|
41
|
+
def dump_all(input_objects, object_to_fill=nil)
|
42
|
+
|
43
|
+
each_object_with_converter(input_objects) do |converter, input_object|
|
44
|
+
object_to_fill = converter.dump(input_object, object_to_fill)
|
45
|
+
end
|
46
|
+
|
47
|
+
self.last_result = object_to_fill
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
52
|
+
def converter=(new_converter)
|
53
|
+
@converter = new_converter.map{|converter| converter.is_a?(Symbol) ? MapperChain.registry.for(converter) : converter}
|
54
|
+
end
|
55
|
+
|
56
|
+
def converter_iterator
|
57
|
+
@converter_iterator ||= self.converter.each
|
58
|
+
end
|
59
|
+
|
60
|
+
def each_object_with_converter(input_objects)
|
61
|
+
it_input_objects = input_objects.each
|
62
|
+
it_converters = self.converter.each
|
63
|
+
|
64
|
+
loop do
|
65
|
+
begin
|
66
|
+
converter = it_converters.next
|
67
|
+
input_object = it_input_objects.next
|
68
|
+
|
69
|
+
yield converter, input_object
|
70
|
+
|
71
|
+
rescue StopIteration
|
72
|
+
break
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
data/lib/yaoc/object_mapper.rb
CHANGED
@@ -18,8 +18,8 @@ module Yaoc
|
|
18
18
|
lazy_loading: nil,
|
19
19
|
reverse_lazy_loading: lazy_loading)
|
20
20
|
|
21
|
-
object_converter = Array(object_converter)
|
22
|
-
reverse_object_converter = Array(reverse_object_converter)
|
21
|
+
object_converter = Array(object_converter).map{|converter| converter.is_a?(Symbol) ? registry.for(converter) : converter}
|
22
|
+
reverse_object_converter = Array(reverse_object_converter).map{|converter| converter.is_a?(Symbol) ? registry.for(converter) : converter}
|
23
23
|
|
24
24
|
converter_builder.rule(
|
25
25
|
to: to,
|
@@ -60,16 +60,21 @@ module Yaoc
|
|
60
60
|
->(_, result){ result }
|
61
61
|
end
|
62
62
|
|
63
|
+
def register_as(name)
|
64
|
+
registry.add(name, self)
|
65
|
+
end
|
66
|
+
|
63
67
|
end
|
64
68
|
|
65
69
|
class ObjectMapper
|
66
70
|
include MapperDSLMethods
|
67
71
|
|
68
|
-
attr_accessor :load_result_source, :dump_result_source
|
72
|
+
attr_accessor :load_result_source, :dump_result_source, :registry
|
69
73
|
|
70
|
-
def initialize(load_result_source, dump_result_source=nil)
|
74
|
+
def initialize(load_result_source, dump_result_source=nil, registry=Yaoc::MapperRegistry)
|
71
75
|
self.load_result_source = load_result_source
|
72
76
|
self.dump_result_source = dump_result_source
|
77
|
+
self.registry = registry
|
73
78
|
end
|
74
79
|
|
75
80
|
def load(fetch_able, object_to_fill=nil)
|
@@ -88,8 +93,20 @@ module Yaoc
|
|
88
93
|
reverse_converter_builder.converter(fetch_able, dump_result_source)
|
89
94
|
end
|
90
95
|
|
96
|
+
def to_s
|
97
|
+
"#{dump_result_source_name} <=> #{load_result_source_name}"
|
98
|
+
end
|
99
|
+
|
91
100
|
protected
|
92
101
|
|
102
|
+
def dump_result_source_name
|
103
|
+
dump_result_source.respond_to?(:name) ? dump_result_source.name : dump_result_source.to_s
|
104
|
+
end
|
105
|
+
|
106
|
+
def load_result_source_name
|
107
|
+
load_result_source.respond_to?(:name) ? load_result_source.name : load_result_source.to_s
|
108
|
+
end
|
109
|
+
|
93
110
|
def apply_commands!
|
94
111
|
converter_builder.apply_commands!
|
95
112
|
reverse_converter_builder.apply_commands!
|
data/lib/yaoc/version.rb
CHANGED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
feature 'Map multiple objects to one', %q{
|
4
|
+
In order to convert objects in lesser steps
|
5
|
+
as a lib user
|
6
|
+
I want to be able to chain converters and input objects
|
7
|
+
} do
|
8
|
+
|
9
|
+
given(:mapper_chain){
|
10
|
+
Yaoc::MapperChain.new(first_mapper, second_mapper)
|
11
|
+
}
|
12
|
+
|
13
|
+
given!(:first_mapper){
|
14
|
+
Yaoc::ObjectMapper.new(new_user_class, old_user_class).tap do |mapper|
|
15
|
+
mapper.add_mapping do
|
16
|
+
register_as :first_mapper
|
17
|
+
fetcher :public_send
|
18
|
+
rule to: :id
|
19
|
+
end
|
20
|
+
end
|
21
|
+
}
|
22
|
+
|
23
|
+
given!(:second_mapper){
|
24
|
+
Yaoc::ObjectMapper.new(new_user_class, old_user_class).tap do |mapper|
|
25
|
+
mapper.add_mapping do
|
26
|
+
register_as :second_mapper
|
27
|
+
fetcher :public_send
|
28
|
+
rule to: :names
|
29
|
+
end
|
30
|
+
end
|
31
|
+
}
|
32
|
+
|
33
|
+
given(:new_user_class){
|
34
|
+
Yaoc::Helper::StructHE(:id, :names)
|
35
|
+
}
|
36
|
+
|
37
|
+
given(:old_user_class){
|
38
|
+
Yaoc::Helper::StructHE(:id, :names)
|
39
|
+
}
|
40
|
+
|
41
|
+
given(:existing_old_user){
|
42
|
+
old_user_class.new(
|
43
|
+
id: 'existing_user_2',
|
44
|
+
names: ['first_name', 'second_name']
|
45
|
+
)
|
46
|
+
}
|
47
|
+
|
48
|
+
given(:existing_user){
|
49
|
+
new_user_class.new(
|
50
|
+
id: 'existing_user_2',
|
51
|
+
names: ['first_name', 'second_name']
|
52
|
+
)
|
53
|
+
}
|
54
|
+
|
55
|
+
scenario 'loads an result object from multiple input object' do
|
56
|
+
converted_user = mapper_chain.load_all([existing_old_user, existing_old_user])
|
57
|
+
|
58
|
+
expect(converted_user.id).to eq 'existing_user_2'
|
59
|
+
expect(converted_user.names).to eq ['first_name', 'second_name']
|
60
|
+
end
|
61
|
+
|
62
|
+
scenario 'dumps an result object from multiple input object' do
|
63
|
+
converted_user = mapper_chain.dump_all([existing_user, existing_user])
|
64
|
+
|
65
|
+
expect(converted_user.id).to eq 'existing_user_2'
|
66
|
+
expect(converted_user.names).to eq ['first_name', 'second_name']
|
67
|
+
end
|
68
|
+
|
69
|
+
scenario 'symbols as converter' do
|
70
|
+
mapper_chain = Yaoc::MapperChain.new(:first_mapper, :second_mapper)
|
71
|
+
|
72
|
+
converted_user = mapper_chain.load_all([existing_old_user, existing_old_user])
|
73
|
+
|
74
|
+
expect(converted_user.id).to eq 'existing_user_2'
|
75
|
+
expect(converted_user.names).to eq ['first_name', 'second_name']
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Yaoc::Helper::Scope do
|
4
|
+
class DummyStorage
|
5
|
+
def initialize(new_data)
|
6
|
+
@data = new_data
|
7
|
+
end
|
8
|
+
|
9
|
+
def for(*args)
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
def data
|
14
|
+
@data
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
subject{
|
20
|
+
Yaoc::Helper::Scope.new("default", DummyStorage.new(data_double))
|
21
|
+
}
|
22
|
+
|
23
|
+
let(:data_double){
|
24
|
+
{}
|
25
|
+
}
|
26
|
+
|
27
|
+
describe '#[]=' do
|
28
|
+
it 'let me set values' do
|
29
|
+
subject['new_value'] = 123
|
30
|
+
expect(subject.fetch('new_value')).to eq 123
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#[]' do
|
35
|
+
it 'let me fetch values' do
|
36
|
+
subject['new_value'] = 123
|
37
|
+
expect(subject['new_value']).to eq 123
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#clear!' do
|
42
|
+
it 'let me fetch values' do
|
43
|
+
subject['new_value'] = 123
|
44
|
+
subject.clear!
|
45
|
+
expect(subject['new_value']).to be_nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#fetch' do
|
50
|
+
it 'works like hash fetch' do
|
51
|
+
fetched_value = subject.fetch('some_key'){|*args| args}
|
52
|
+
|
53
|
+
expect(fetched_value).to eq ['some_key']
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Yaoc::Helper::ThreadLocalStorage do
|
4
|
+
subject{
|
5
|
+
Yaoc::Helper::ThreadLocalStorage
|
6
|
+
}
|
7
|
+
|
8
|
+
describe '.for' do
|
9
|
+
it 'creates a new object for every thread' do
|
10
|
+
subject.for
|
11
|
+
|
12
|
+
threat_one_object_id = nil
|
13
|
+
threat_two_object_id = nil
|
14
|
+
threats = []
|
15
|
+
|
16
|
+
threats << Thread.new{
|
17
|
+
threat_one_object_id = Yaoc::Helper::ThreadLocalStorage.for
|
18
|
+
}
|
19
|
+
|
20
|
+
threats << Thread.new{
|
21
|
+
threat_two_object_id = Yaoc::Helper::ThreadLocalStorage.for
|
22
|
+
}
|
23
|
+
|
24
|
+
threats.each &:join
|
25
|
+
|
26
|
+
expect(threat_one_object_id).not_to eq threat_two_object_id
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'supports naming different scopes' do
|
30
|
+
second_scope = subject.for 'the second scope'
|
31
|
+
expect(second_scope.object_id).not_to be subject.for.object_id
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Yaoc::MapperChain do
|
4
|
+
|
5
|
+
subject{
|
6
|
+
Yaoc::MapperChain.new(first_mapper, second_mapper)
|
7
|
+
}
|
8
|
+
|
9
|
+
let(:first_mapper){
|
10
|
+
Yaoc::ObjectMapper.new(new_user_class, old_user_class).tap do |mapper|
|
11
|
+
mapper.add_mapping do
|
12
|
+
fetcher :public_send
|
13
|
+
rule to: :id,
|
14
|
+
from: :o_id
|
15
|
+
end
|
16
|
+
end
|
17
|
+
}
|
18
|
+
|
19
|
+
let(:second_mapper){
|
20
|
+
Yaoc::ObjectMapper.new(new_user_class, old_user_class).tap do |mapper|
|
21
|
+
mapper.add_mapping do
|
22
|
+
fetcher :public_send
|
23
|
+
rule to: :names,
|
24
|
+
from: :o_names
|
25
|
+
end
|
26
|
+
end
|
27
|
+
}
|
28
|
+
|
29
|
+
let(:new_user_class){
|
30
|
+
Yaoc::Helper::StructHE(:id, :names)
|
31
|
+
}
|
32
|
+
|
33
|
+
let(:old_user_class){
|
34
|
+
Yaoc::Helper::StructHE(:o_id, :o_names)
|
35
|
+
}
|
36
|
+
|
37
|
+
let(:existing_old_user){
|
38
|
+
old_user_class.new(
|
39
|
+
o_id: 'existing_user_2',
|
40
|
+
o_names: ['first_name', 'second_name']
|
41
|
+
)
|
42
|
+
}
|
43
|
+
|
44
|
+
let(:existing_user){
|
45
|
+
new_user_class.new(
|
46
|
+
id: 'existing_user_2',
|
47
|
+
names: ['first_name', 'second_name']
|
48
|
+
)
|
49
|
+
}
|
50
|
+
|
51
|
+
describe '.new' do
|
52
|
+
subject{
|
53
|
+
Yaoc::MapperChain
|
54
|
+
}
|
55
|
+
|
56
|
+
it 'converts symbols into converter' do
|
57
|
+
registry = double('registry')
|
58
|
+
|
59
|
+
expect(registry).to receive(:for).with(:one)
|
60
|
+
expect(registry).to receive(:for).with(:two)
|
61
|
+
expect(registry).to receive(:for).with(:three)
|
62
|
+
|
63
|
+
subject.stub(registry: registry)
|
64
|
+
|
65
|
+
subject.new(:one, :two, :three)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#load_all' do
|
70
|
+
it 'converts multiple input objects into one result object' do
|
71
|
+
converted_user = subject.load_all([existing_old_user, existing_old_user])
|
72
|
+
|
73
|
+
expect(converted_user.id).to eq 'existing_user_2'
|
74
|
+
expect(converted_user.names).to eq ['first_name', 'second_name']
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'fills an existing object' do
|
78
|
+
user = new_user_class.new
|
79
|
+
|
80
|
+
converted_user = subject.load_all([existing_old_user, existing_old_user], user)
|
81
|
+
|
82
|
+
expect(converted_user.id).to eq 'existing_user_2'
|
83
|
+
expect(converted_user.names).to eq ['first_name', 'second_name']
|
84
|
+
|
85
|
+
expect(converted_user.object_id).to eq user.object_id
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe '#load_first' do
|
90
|
+
it 'converts the first object into the result object' do
|
91
|
+
converted_user = subject.load_first(existing_old_user)
|
92
|
+
|
93
|
+
expect(converted_user.id).to eq 'existing_user_2'
|
94
|
+
expect(converted_user.names).to be_nil
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe '#load_next' do
|
99
|
+
it 'converts the next object into the result object' do
|
100
|
+
subject.load_first(existing_old_user)
|
101
|
+
converted_user = subject.load_next(existing_old_user)
|
102
|
+
|
103
|
+
expect(converted_user.id).to eq 'existing_user_2'
|
104
|
+
expect(converted_user.names).to eq ["first_name", "second_name"]
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'raises an exception when too many values are passed' do
|
108
|
+
subject.load_first(existing_old_user)
|
109
|
+
subject.load_next(existing_old_user)
|
110
|
+
|
111
|
+
expect{subject.load_next(existing_old_user)}.to raise_error "ToManyInputObjects"
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
describe '#dump_all' do
|
117
|
+
it 'converts multiple input objects into one result object' do
|
118
|
+
converted_user = subject.dump_all([existing_user, existing_user])
|
119
|
+
|
120
|
+
expect(converted_user.o_id).to eq 'existing_user_2'
|
121
|
+
expect(converted_user.o_names).to eq ['first_name', 'second_name']
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'fills an existing object' do
|
125
|
+
user = old_user_class.new
|
126
|
+
converted_user = subject.dump_all([existing_user, existing_user], user)
|
127
|
+
|
128
|
+
expect(converted_user.o_id).to eq 'existing_user_2'
|
129
|
+
expect(converted_user.o_names).to eq ['first_name', 'second_name']
|
130
|
+
|
131
|
+
expect(converted_user.object_id).to eq user.object_id
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
describe '#dump_first' do
|
137
|
+
it 'dumps the first object into the result object' do
|
138
|
+
converted_user = subject.dump_first(existing_user)
|
139
|
+
|
140
|
+
expect(converted_user.o_id).to eq 'existing_user_2'
|
141
|
+
expect(converted_user.o_names).to be_nil
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe '#dump_next' do
|
146
|
+
it 'dumps the next object into the result object' do
|
147
|
+
subject.dump_first(existing_user)
|
148
|
+
converted_user = subject.dump_next(existing_user)
|
149
|
+
|
150
|
+
expect(converted_user.o_id).to eq 'existing_user_2'
|
151
|
+
expect(converted_user.o_names).to eq ['first_name', 'second_name']
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Yaoc::MapperRegistry do
|
4
|
+
subject{
|
5
|
+
Yaoc::MapperRegistry
|
6
|
+
}
|
7
|
+
|
8
|
+
describe '#add' do
|
9
|
+
it "registers an object" do
|
10
|
+
subject.add(:my_key, Object)
|
11
|
+
expect(subject.for(:my_key)).to eq Object
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#for' do
|
16
|
+
it "returns the registered object" do
|
17
|
+
subject.add(:my_key, Object)
|
18
|
+
expect(subject.for(:my_key)).to eq Object
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -193,6 +193,18 @@ describe Yaoc::ObjectMapper do
|
|
193
193
|
end
|
194
194
|
end
|
195
195
|
|
196
|
+
it 'allows to register the mapper globaly' do
|
197
|
+
registry_double=double('registry')
|
198
|
+
subject.registry = registry_double
|
199
|
+
|
200
|
+
expect(registry_double).to receive(:add).with(:mapper_name, subject)
|
201
|
+
|
202
|
+
subject.add_mapping do
|
203
|
+
register_as :mapper_name
|
204
|
+
end
|
205
|
+
|
206
|
+
end
|
207
|
+
|
196
208
|
|
197
209
|
end
|
198
210
|
|
@@ -236,4 +248,12 @@ describe Yaoc::ObjectMapper do
|
|
236
248
|
end
|
237
249
|
end
|
238
250
|
|
251
|
+
describe '#to_s' do
|
252
|
+
it 'returns a readable representation' do
|
253
|
+
subject.dump_result_source = Object
|
254
|
+
subject.load_result_source = nil
|
255
|
+
expect(subject.to_s).to eq "Object <=> "
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
239
259
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaoc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dieter Späth
|
@@ -215,8 +215,12 @@ files:
|
|
215
215
|
- examples/all_examples.rb
|
216
216
|
- lib/yaoc.rb
|
217
217
|
- lib/yaoc/converter_builder.rb
|
218
|
+
- lib/yaoc/helper/scope.rb
|
218
219
|
- lib/yaoc/helper/struct_hash_constructor.rb
|
220
|
+
- lib/yaoc/helper/thread_local_storage.rb
|
219
221
|
- lib/yaoc/helper/to_proc_delegator.rb
|
222
|
+
- lib/yaoc/mapper_chain.rb
|
223
|
+
- lib/yaoc/mapper_registry.rb
|
220
224
|
- lib/yaoc/mapping_base.rb
|
221
225
|
- lib/yaoc/mapping_to_class.rb
|
222
226
|
- lib/yaoc/object_mapper.rb
|
@@ -226,16 +230,22 @@ files:
|
|
226
230
|
- lib/yaoc/transformation_deferred_command.rb
|
227
231
|
- lib/yaoc/version.rb
|
228
232
|
- spec/acceptance/fill_existing_objects_spec.rb
|
233
|
+
- spec/acceptance/map_multiple_objects_to_one_in_a_chain_spec.rb
|
229
234
|
- spec/acceptance/map_objects_spec.rb
|
230
235
|
- spec/acceptance/map_to_objects_using_other_converters_spec.rb
|
231
236
|
- spec/acceptance/map_to_objects_with_lazy_loading_spec.rb
|
232
237
|
- spec/acceptance/map_to_objects_with_positional_constructors_spec.rb
|
233
238
|
- spec/integration/lib/yaoc/converter_builder_spec.rb
|
239
|
+
- spec/integration/lib/yaoc/helper/scope_spec.rb
|
234
240
|
- spec/spec_helper.rb
|
235
241
|
- spec/support/feature.rb
|
236
242
|
- spec/unit/lib/yaoc/converter_builder_spec.rb
|
243
|
+
- spec/unit/lib/yaoc/helper/scope_spec.rb
|
237
244
|
- spec/unit/lib/yaoc/helper/struct_hash_constructor_spec.rb
|
245
|
+
- spec/unit/lib/yaoc/helper/thread_local_storage_spec.rb
|
238
246
|
- spec/unit/lib/yaoc/helper/to_proc_delegator_spec.rb
|
247
|
+
- spec/unit/lib/yaoc/mapper_chain_spec.rb
|
248
|
+
- spec/unit/lib/yaoc/mapper_registry_spec.rb
|
239
249
|
- spec/unit/lib/yaoc/mapping_base_spec.rb
|
240
250
|
- spec/unit/lib/yaoc/mapping_to_class_spec.rb
|
241
251
|
- spec/unit/lib/yaoc/object_mapper_spec.rb
|
@@ -270,16 +280,22 @@ specification_version: 4
|
|
270
280
|
summary: Yet another object converter
|
271
281
|
test_files:
|
272
282
|
- spec/acceptance/fill_existing_objects_spec.rb
|
283
|
+
- spec/acceptance/map_multiple_objects_to_one_in_a_chain_spec.rb
|
273
284
|
- spec/acceptance/map_objects_spec.rb
|
274
285
|
- spec/acceptance/map_to_objects_using_other_converters_spec.rb
|
275
286
|
- spec/acceptance/map_to_objects_with_lazy_loading_spec.rb
|
276
287
|
- spec/acceptance/map_to_objects_with_positional_constructors_spec.rb
|
277
288
|
- spec/integration/lib/yaoc/converter_builder_spec.rb
|
289
|
+
- spec/integration/lib/yaoc/helper/scope_spec.rb
|
278
290
|
- spec/spec_helper.rb
|
279
291
|
- spec/support/feature.rb
|
280
292
|
- spec/unit/lib/yaoc/converter_builder_spec.rb
|
293
|
+
- spec/unit/lib/yaoc/helper/scope_spec.rb
|
281
294
|
- spec/unit/lib/yaoc/helper/struct_hash_constructor_spec.rb
|
295
|
+
- spec/unit/lib/yaoc/helper/thread_local_storage_spec.rb
|
282
296
|
- spec/unit/lib/yaoc/helper/to_proc_delegator_spec.rb
|
297
|
+
- spec/unit/lib/yaoc/mapper_chain_spec.rb
|
298
|
+
- spec/unit/lib/yaoc/mapper_registry_spec.rb
|
283
299
|
- spec/unit/lib/yaoc/mapping_base_spec.rb
|
284
300
|
- spec/unit/lib/yaoc/mapping_to_class_spec.rb
|
285
301
|
- spec/unit/lib/yaoc/object_mapper_spec.rb
|