yaoc 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +62 -2
- data/examples/05_fill_existing_objects.rb +1 -1
- data/lib/yaoc.rb +3 -1
- data/lib/yaoc/helper/scope.rb +2 -2
- data/lib/yaoc/helper/thread_global_storage.rb +56 -0
- data/lib/yaoc/helper/thread_local_storage.rb +17 -0
- data/lib/yaoc/{mapper_chain.rb → many_to_one_mapper_chain.rb} +2 -2
- data/lib/yaoc/mapper_registry.rb +4 -0
- data/lib/yaoc/mapping_base.rb +2 -0
- data/lib/yaoc/mapping_to_class.rb +2 -1
- data/lib/yaoc/one_to_many_mapper_chain.rb +51 -0
- data/lib/yaoc/version.rb +1 -1
- data/spec/acceptance/map_multiple_objects_to_one_in_a_chain_spec.rb +2 -2
- data/spec/acceptance/map_one_object_to_many_in_a_chain_spec.rb +78 -0
- data/spec/unit/lib/yaoc/helper/scope_spec.rb +1 -1
- data/spec/unit/lib/yaoc/helper/thread_global_storage_spec.rb +49 -0
- data/spec/unit/lib/yaoc/helper/thread_local_storage_spec.rb +15 -0
- data/spec/unit/lib/yaoc/{mapper_chain_spec.rb → many_to_one_mapper_chain_spec.rb} +3 -3
- data/spec/unit/lib/yaoc/mapper_registry_spec.rb +10 -2
- data/spec/unit/lib/yaoc/one_to_many_mapper_chain_spec.rb +113 -0
- metadata +13 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1d8b71655a2f448f78445bad0780255aabe5dbd
|
4
|
+
data.tar.gz: 5888fd8a52a6aed1903541425bb32d8d74352343
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83823a34c900b9a1c7f072a8668b2d7b271996777b49f20e8be0108daab48e2cfef16ce885c83dcce4ade8dc85797995fd24d6beec15306e9ce1aeec180ce97c
|
7
|
+
data.tar.gz: dad524584b6d519d1e1b83e79f139c0343afe38667e804b7b868ddda4ae681ac73b4018817e43a89b619eb4f737c486d2bcd063b16af34db271f03fedd8c1fec
|
data/README.md
CHANGED
@@ -18,7 +18,67 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
### What does it do in a high level?
|
22
|
+
|
23
|
+
You say transform an object of class A into an object of class B:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
mapper =Yaoc::ObjectMapper.new(B, A) # yes, first what you want, then where to get it
|
27
|
+
```
|
28
|
+
|
29
|
+
Then you define some rules how it should be done:
|
30
|
+
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
mapper.add_mapping do
|
34
|
+
fetcher :public_send
|
35
|
+
strategy :to_hash_mapping # default or :to_array_mapping
|
36
|
+
|
37
|
+
rule to: :role, from: :r_role
|
38
|
+
|
39
|
+
rule to: :firstname,
|
40
|
+
from: :fullname,
|
41
|
+
converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :firstname, source.fullname.split().first) },
|
42
|
+
reverse_converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :fullname, "#{source.firstname} #{source.lastname}") }
|
43
|
+
|
44
|
+
rule to: :lastname,
|
45
|
+
from: :fullname,
|
46
|
+
converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :lastname, source.fullname.split().last ) },
|
47
|
+
reverse_converter: ->(source, result){ result }
|
48
|
+
|
49
|
+
rule to: :id
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
After this you can transform an object of class A into an object of class B:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
mapper.load(A.new)
|
57
|
+
```
|
58
|
+
|
59
|
+
|
60
|
+
Or reverse:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
mapper.dump(B.new)
|
64
|
+
```
|
65
|
+
|
66
|
+
Depending on ```strategy``` or ```reverse_strategy``` the input object is first
|
67
|
+
transformed into a Hash or Array and after this passed to the class constructor.
|
68
|
+
|
69
|
+
You can also pass an existing object to load and dump:
|
70
|
+
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
b = B.new
|
74
|
+
mapper.load(A.new, b)
|
75
|
+
|
76
|
+
a = A.new
|
77
|
+
mapper.dump(B.new, a)
|
78
|
+
```
|
79
|
+
|
80
|
+
In this case setters with matching name to the hash key
|
81
|
+
are invoked with the hash value as value.
|
22
82
|
|
23
83
|
### The resulting classes have hash enabled constructors?
|
24
84
|
|
@@ -312,7 +372,7 @@ role_mapper.load(old_role, new_user5)
|
|
312
372
|
|
313
373
|
# OR
|
314
374
|
#
|
315
|
-
# mapper_chain = Yaoc::
|
375
|
+
# mapper_chain = Yaoc::ManyToOneMapperChain.new(user_mapper, role_mapper)
|
316
376
|
# new_user5 = mapper_chain.load_all([old_user5, old_role])
|
317
377
|
|
318
378
|
|
data/lib/yaoc.rb
CHANGED
@@ -3,6 +3,7 @@ require "yaoc/version"
|
|
3
3
|
require 'yaoc/helper/struct_hash_constructor'
|
4
4
|
require 'yaoc/helper/to_proc_delegator'
|
5
5
|
require 'yaoc/helper/thread_local_storage'
|
6
|
+
require 'yaoc/helper/thread_global_storage'
|
6
7
|
require 'yaoc/helper/scope'
|
7
8
|
|
8
9
|
require 'yaoc/mapper_registry'
|
@@ -15,7 +16,8 @@ Dir[File.join(File.expand_path(__dir__ ), "yaoc/strategies/*.rb")].each { |f| re
|
|
15
16
|
|
16
17
|
require 'yaoc/converter_builder'
|
17
18
|
require 'yaoc/object_mapper'
|
18
|
-
require 'yaoc/
|
19
|
+
require 'yaoc/many_to_one_mapper_chain'
|
20
|
+
require 'yaoc/one_to_many_mapper_chain'
|
19
21
|
|
20
22
|
module Yaoc
|
21
23
|
|
data/lib/yaoc/helper/scope.rb
CHANGED
@@ -4,13 +4,13 @@ module Yaoc
|
|
4
4
|
|
5
5
|
attr_accessor :scope_name, :storage_source
|
6
6
|
|
7
|
-
def initialize(scope_name="default", storage_source=
|
7
|
+
def initialize(scope_name="default", storage_source=ThreadGlobalStorage)
|
8
8
|
self.scope_name = scope_name
|
9
9
|
self.storage_source = storage_source
|
10
10
|
end
|
11
11
|
|
12
12
|
def storage
|
13
|
-
storage_source.for(scope_name)
|
13
|
+
storage_source.for(scope_name)
|
14
14
|
end
|
15
15
|
|
16
16
|
def []=(key, value)
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Yaoc
|
2
|
+
module Helper
|
3
|
+
require 'thread'
|
4
|
+
|
5
|
+
class ThreadGlobalStorage
|
6
|
+
class << self
|
7
|
+
private :new
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_accessor :data
|
11
|
+
|
12
|
+
@mutex = Mutex.new
|
13
|
+
|
14
|
+
def self.mutex
|
15
|
+
@mutex
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.for(scope_name="default")
|
19
|
+
mutex.synchronize {
|
20
|
+
@storage ||= {}
|
21
|
+
@storage[scope_name] ||= new
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
self.data||={}
|
27
|
+
end
|
28
|
+
|
29
|
+
def []=(key, value)
|
30
|
+
mutex.synchronize {
|
31
|
+
self.data[key]=value
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
def [](key)
|
36
|
+
self.data[key]
|
37
|
+
end
|
38
|
+
|
39
|
+
def clear!
|
40
|
+
mutex.synchronize {
|
41
|
+
self.data.clear
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def fetch(*args, &block)
|
46
|
+
self.data.fetch(*args, &block)
|
47
|
+
end
|
48
|
+
|
49
|
+
def mutex
|
50
|
+
self.class.mutex
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -14,6 +14,23 @@ module Yaoc
|
|
14
14
|
def initialize
|
15
15
|
self.data||={}
|
16
16
|
end
|
17
|
+
|
18
|
+
def []=(key, value)
|
19
|
+
self.data[key]=value
|
20
|
+
end
|
21
|
+
|
22
|
+
def [](key)
|
23
|
+
self.data[key]
|
24
|
+
end
|
25
|
+
|
26
|
+
def clear!
|
27
|
+
self.data.clear
|
28
|
+
end
|
29
|
+
|
30
|
+
def fetch(*args, &block)
|
31
|
+
self.data.fetch(*args, &block)
|
32
|
+
end
|
33
|
+
|
17
34
|
end
|
18
35
|
end
|
19
36
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module Yaoc
|
2
|
-
class
|
2
|
+
class ManyToOneMapperChain
|
3
3
|
attr_accessor :converter, :last_result, :next_result
|
4
4
|
|
5
5
|
def self.registry
|
@@ -50,7 +50,7 @@ module Yaoc
|
|
50
50
|
protected
|
51
51
|
|
52
52
|
def converter=(new_converter)
|
53
|
-
@converter = new_converter.map{|converter| converter.is_a?(Symbol) ?
|
53
|
+
@converter = new_converter.map{|converter| converter.is_a?(Symbol) ? ManyToOneMapperChain.registry.for(converter) : converter}
|
54
54
|
end
|
55
55
|
|
56
56
|
def converter_iterator
|
data/lib/yaoc/mapper_registry.rb
CHANGED
data/lib/yaoc/mapping_base.rb
CHANGED
@@ -8,7 +8,7 @@ module Yaoc
|
|
8
8
|
|
9
9
|
module InstanceMethods
|
10
10
|
def call(pre_created_object=nil)
|
11
|
-
source_converted_to_hash_or_array =
|
11
|
+
source_converted_to_hash_or_array = to_hash_or_array()
|
12
12
|
unless source_converted_to_hash_or_array.nil?
|
13
13
|
if pre_created_object.nil?
|
14
14
|
create_target_from_class(source_converted_to_hash_or_array)
|
@@ -19,6 +19,7 @@ module Yaoc
|
|
19
19
|
pre_created_object
|
20
20
|
end
|
21
21
|
end
|
22
|
+
alias_method :to_object, :call
|
22
23
|
|
23
24
|
def source_method
|
24
25
|
self.target_source.respond_to?(:call) ? :call : :new
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Yaoc
|
2
|
+
class OneToManyMapperChain
|
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_all(input_object, objects_to_fill=[])
|
14
|
+
objects_to_fill = Array(objects_to_fill)
|
15
|
+
results = []
|
16
|
+
|
17
|
+
each_object_with_converter(objects_to_fill) do |converter, object_to_fill|
|
18
|
+
results << converter.load(input_object, object_to_fill)
|
19
|
+
end
|
20
|
+
|
21
|
+
self.last_result = results
|
22
|
+
end
|
23
|
+
|
24
|
+
def dump_all(input_object, objects_to_fill=nil)
|
25
|
+
objects_to_fill = Array(objects_to_fill)
|
26
|
+
results = []
|
27
|
+
|
28
|
+
each_object_with_converter(objects_to_fill) do |converter, object_to_fill|
|
29
|
+
results << converter.dump(input_object, object_to_fill)
|
30
|
+
end
|
31
|
+
|
32
|
+
self.last_result = results
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def converter=(new_converter)
|
38
|
+
@converter = new_converter.map{|converter| converter.is_a?(Symbol) ? OneToManyMapperChain.registry.for(converter) : converter}
|
39
|
+
end
|
40
|
+
|
41
|
+
def each_object_with_converter(objects_to_fill)
|
42
|
+
|
43
|
+
converter.each_with_index do |converter, index|
|
44
|
+
object_to_fill = objects_to_fill[index]
|
45
|
+
|
46
|
+
yield converter, object_to_fill
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
data/lib/yaoc/version.rb
CHANGED
@@ -7,7 +7,7 @@ feature 'Map multiple objects to one', %q{
|
|
7
7
|
} do
|
8
8
|
|
9
9
|
given(:mapper_chain){
|
10
|
-
Yaoc::
|
10
|
+
Yaoc::ManyToOneMapperChain.new(first_mapper, second_mapper)
|
11
11
|
}
|
12
12
|
|
13
13
|
given!(:first_mapper){
|
@@ -67,7 +67,7 @@ feature 'Map multiple objects to one', %q{
|
|
67
67
|
end
|
68
68
|
|
69
69
|
scenario 'symbols as converter' do
|
70
|
-
mapper_chain = Yaoc::
|
70
|
+
mapper_chain = Yaoc::ManyToOneMapperChain.new(:first_mapper, :second_mapper)
|
71
71
|
|
72
72
|
converted_user = mapper_chain.load_all([existing_old_user, existing_old_user])
|
73
73
|
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
feature 'Map multiple one object to many', %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 get multiple objects out of one
|
7
|
+
} do
|
8
|
+
|
9
|
+
given(:mapper_chain){
|
10
|
+
Yaoc::OneToManyMapperChain.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 multiple result object from one input object' do
|
56
|
+
converted_users = mapper_chain.load_all(existing_old_user)
|
57
|
+
|
58
|
+
expect(converted_users[0].id).to eq 'existing_user_2'
|
59
|
+
expect(converted_users[1].names).to eq ['first_name', 'second_name']
|
60
|
+
end
|
61
|
+
|
62
|
+
scenario 'dumps multiple result object from one input object' do
|
63
|
+
converted_users = mapper_chain.dump_all(existing_user)
|
64
|
+
|
65
|
+
expect(converted_users[0].id).to eq 'existing_user_2'
|
66
|
+
expect(converted_users[1].names).to eq ['first_name', 'second_name']
|
67
|
+
end
|
68
|
+
|
69
|
+
scenario 'symbols as converter' do
|
70
|
+
mapper_chain = Yaoc::OneToManyMapperChain.new(:first_mapper, :second_mapper)
|
71
|
+
|
72
|
+
converted_users = mapper_chain.load_all(existing_old_user)
|
73
|
+
|
74
|
+
expect(converted_users[0].id).to eq 'existing_user_2'
|
75
|
+
expect(converted_users[1].names).to eq ['first_name', 'second_name']
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Yaoc::Helper::ThreadGlobalStorage do
|
4
|
+
subject{
|
5
|
+
Yaoc::Helper::ThreadGlobalStorage
|
6
|
+
}
|
7
|
+
|
8
|
+
let(:storage){
|
9
|
+
subject.for
|
10
|
+
}
|
11
|
+
|
12
|
+
describe '.for' do
|
13
|
+
it 'creates a new object for all threads' do
|
14
|
+
subject.for
|
15
|
+
|
16
|
+
threat_one_object_id = nil
|
17
|
+
threat_two_object_id = nil
|
18
|
+
threats = []
|
19
|
+
|
20
|
+
threats << Thread.new{
|
21
|
+
threat_one_object_id = Yaoc::Helper::ThreadLocalStorage.for
|
22
|
+
}
|
23
|
+
|
24
|
+
threats << Thread.new{
|
25
|
+
threat_two_object_id = Yaoc::Helper::ThreadLocalStorage.for
|
26
|
+
}
|
27
|
+
|
28
|
+
threats.each &:join
|
29
|
+
|
30
|
+
expect(threat_one_object_id).not_to eq threat_two_object_id
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'supports naming different scopes' do
|
34
|
+
second_scope = subject.for 'the second scope'
|
35
|
+
expect(second_scope.object_id).not_to be subject.for.object_id
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'supports hash like accessors' do
|
40
|
+
storage['key'] = 1
|
41
|
+
expect(storage['key']).to eq 1
|
42
|
+
expect(storage.fetch 'key').to eq 1
|
43
|
+
|
44
|
+
storage.clear!
|
45
|
+
|
46
|
+
expect(storage['key']).to be_nil
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -5,6 +5,10 @@ describe Yaoc::Helper::ThreadLocalStorage do
|
|
5
5
|
Yaoc::Helper::ThreadLocalStorage
|
6
6
|
}
|
7
7
|
|
8
|
+
let(:storage){
|
9
|
+
subject.for
|
10
|
+
}
|
11
|
+
|
8
12
|
describe '.for' do
|
9
13
|
it 'creates a new object for every thread' do
|
10
14
|
subject.for
|
@@ -32,4 +36,15 @@ describe Yaoc::Helper::ThreadLocalStorage do
|
|
32
36
|
end
|
33
37
|
end
|
34
38
|
|
39
|
+
it 'supports hash like accessors' do
|
40
|
+
storage['key'] = 1
|
41
|
+
expect(storage['key']).to eq 1
|
42
|
+
expect(storage.fetch 'key').to eq 1
|
43
|
+
|
44
|
+
storage.clear!
|
45
|
+
|
46
|
+
expect(storage['key']).to be_nil
|
47
|
+
|
48
|
+
end
|
49
|
+
|
35
50
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Yaoc::
|
3
|
+
describe Yaoc::ManyToOneMapperChain do
|
4
4
|
|
5
5
|
subject{
|
6
|
-
Yaoc::
|
6
|
+
Yaoc::ManyToOneMapperChain.new(first_mapper, second_mapper)
|
7
7
|
}
|
8
8
|
|
9
9
|
let(:first_mapper){
|
@@ -50,7 +50,7 @@ describe Yaoc::MapperChain do
|
|
50
50
|
|
51
51
|
describe '.new' do
|
52
52
|
subject{
|
53
|
-
Yaoc::
|
53
|
+
Yaoc::ManyToOneMapperChain
|
54
54
|
}
|
55
55
|
|
56
56
|
it 'converts symbols into converter' do
|
@@ -5,17 +5,25 @@ describe Yaoc::MapperRegistry do
|
|
5
5
|
Yaoc::MapperRegistry
|
6
6
|
}
|
7
7
|
|
8
|
-
describe '
|
8
|
+
describe '.add' do
|
9
9
|
it "registers an object" do
|
10
10
|
subject.add(:my_key, Object)
|
11
11
|
expect(subject.for(:my_key)).to eq Object
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
describe '
|
15
|
+
describe '.for' do
|
16
16
|
it "returns the registered object" do
|
17
17
|
subject.add(:my_key, Object)
|
18
18
|
expect(subject.for(:my_key)).to eq Object
|
19
19
|
end
|
20
20
|
end
|
21
|
+
|
22
|
+
describe '.scope_storage' do
|
23
|
+
it 'supports the change of scope storage' do
|
24
|
+
expect {subject.scope_storage(Yaoc::Helper::ThreadLocalStorage)}.not_to raise_error
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
21
29
|
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Yaoc::OneToManyMapperChain do
|
4
|
+
|
5
|
+
subject{
|
6
|
+
Yaoc::OneToManyMapperChain.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::ManyToOneMapperChain
|
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 one input objects into multiple result object' do
|
71
|
+
converted_users = subject.load_all(existing_old_user)
|
72
|
+
|
73
|
+
expect(converted_users[0].id).to eq 'existing_user_2'
|
74
|
+
expect(converted_users[1].names).to eq ['first_name', 'second_name']
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'fills an existing objects' do
|
78
|
+
user1 = new_user_class.new
|
79
|
+
user2 = new_user_class.new
|
80
|
+
|
81
|
+
converted_users = subject.load_all(existing_old_user, [user1, user2])
|
82
|
+
|
83
|
+
expect(converted_users[0].id).to eq 'existing_user_2'
|
84
|
+
expect(converted_users[1].names).to eq ['first_name', 'second_name']
|
85
|
+
|
86
|
+
expect(converted_users[0].object_id).to eq user1.object_id
|
87
|
+
expect(converted_users[1].object_id).to eq user2.object_id
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
describe '#dump_all' do
|
93
|
+
it 'converts one input object into multiple result object' do
|
94
|
+
converted_users = subject.dump_all(existing_user)
|
95
|
+
|
96
|
+
expect(converted_users[0].o_id).to eq 'existing_user_2'
|
97
|
+
expect(converted_users[1].o_names).to eq ['first_name', 'second_name']
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'fills an existing objects' do
|
101
|
+
user1 = old_user_class.new
|
102
|
+
user2 = old_user_class.new
|
103
|
+
converted_users = subject.dump_all(existing_user, [user1, user2])
|
104
|
+
|
105
|
+
expect(converted_users[0].o_id).to eq 'existing_user_2'
|
106
|
+
expect(converted_users[1].o_names).to eq ['first_name', 'second_name']
|
107
|
+
|
108
|
+
expect(converted_users[0].object_id).to eq user1.object_id
|
109
|
+
expect(converted_users[1].object_id).to eq user2.object_id
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dieter Späth
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -217,13 +217,15 @@ files:
|
|
217
217
|
- lib/yaoc/converter_builder.rb
|
218
218
|
- lib/yaoc/helper/scope.rb
|
219
219
|
- lib/yaoc/helper/struct_hash_constructor.rb
|
220
|
+
- lib/yaoc/helper/thread_global_storage.rb
|
220
221
|
- lib/yaoc/helper/thread_local_storage.rb
|
221
222
|
- lib/yaoc/helper/to_proc_delegator.rb
|
222
|
-
- lib/yaoc/
|
223
|
+
- lib/yaoc/many_to_one_mapper_chain.rb
|
223
224
|
- lib/yaoc/mapper_registry.rb
|
224
225
|
- lib/yaoc/mapping_base.rb
|
225
226
|
- lib/yaoc/mapping_to_class.rb
|
226
227
|
- lib/yaoc/object_mapper.rb
|
228
|
+
- lib/yaoc/one_to_many_mapper_chain.rb
|
227
229
|
- lib/yaoc/strategies/to_array_mapping.rb
|
228
230
|
- lib/yaoc/strategies/to_hash_mapping.rb
|
229
231
|
- lib/yaoc/transformation_command.rb
|
@@ -232,6 +234,7 @@ files:
|
|
232
234
|
- spec/acceptance/fill_existing_objects_spec.rb
|
233
235
|
- spec/acceptance/map_multiple_objects_to_one_in_a_chain_spec.rb
|
234
236
|
- spec/acceptance/map_objects_spec.rb
|
237
|
+
- spec/acceptance/map_one_object_to_many_in_a_chain_spec.rb
|
235
238
|
- spec/acceptance/map_to_objects_using_other_converters_spec.rb
|
236
239
|
- spec/acceptance/map_to_objects_with_lazy_loading_spec.rb
|
237
240
|
- spec/acceptance/map_to_objects_with_positional_constructors_spec.rb
|
@@ -242,13 +245,15 @@ files:
|
|
242
245
|
- spec/unit/lib/yaoc/converter_builder_spec.rb
|
243
246
|
- spec/unit/lib/yaoc/helper/scope_spec.rb
|
244
247
|
- spec/unit/lib/yaoc/helper/struct_hash_constructor_spec.rb
|
248
|
+
- spec/unit/lib/yaoc/helper/thread_global_storage_spec.rb
|
245
249
|
- spec/unit/lib/yaoc/helper/thread_local_storage_spec.rb
|
246
250
|
- spec/unit/lib/yaoc/helper/to_proc_delegator_spec.rb
|
247
|
-
- spec/unit/lib/yaoc/
|
251
|
+
- spec/unit/lib/yaoc/many_to_one_mapper_chain_spec.rb
|
248
252
|
- spec/unit/lib/yaoc/mapper_registry_spec.rb
|
249
253
|
- spec/unit/lib/yaoc/mapping_base_spec.rb
|
250
254
|
- spec/unit/lib/yaoc/mapping_to_class_spec.rb
|
251
255
|
- spec/unit/lib/yaoc/object_mapper_spec.rb
|
256
|
+
- spec/unit/lib/yaoc/one_to_many_mapper_chain_spec.rb
|
252
257
|
- spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb
|
253
258
|
- spec/unit/lib/yaoc/strategies/to_hash_mapping_spec.rb
|
254
259
|
- spec/unit/lib/yaoc/transformation_command_spec.rb
|
@@ -282,6 +287,7 @@ test_files:
|
|
282
287
|
- spec/acceptance/fill_existing_objects_spec.rb
|
283
288
|
- spec/acceptance/map_multiple_objects_to_one_in_a_chain_spec.rb
|
284
289
|
- spec/acceptance/map_objects_spec.rb
|
290
|
+
- spec/acceptance/map_one_object_to_many_in_a_chain_spec.rb
|
285
291
|
- spec/acceptance/map_to_objects_using_other_converters_spec.rb
|
286
292
|
- spec/acceptance/map_to_objects_with_lazy_loading_spec.rb
|
287
293
|
- spec/acceptance/map_to_objects_with_positional_constructors_spec.rb
|
@@ -292,13 +298,15 @@ test_files:
|
|
292
298
|
- spec/unit/lib/yaoc/converter_builder_spec.rb
|
293
299
|
- spec/unit/lib/yaoc/helper/scope_spec.rb
|
294
300
|
- spec/unit/lib/yaoc/helper/struct_hash_constructor_spec.rb
|
301
|
+
- spec/unit/lib/yaoc/helper/thread_global_storage_spec.rb
|
295
302
|
- spec/unit/lib/yaoc/helper/thread_local_storage_spec.rb
|
296
303
|
- spec/unit/lib/yaoc/helper/to_proc_delegator_spec.rb
|
297
|
-
- spec/unit/lib/yaoc/
|
304
|
+
- spec/unit/lib/yaoc/many_to_one_mapper_chain_spec.rb
|
298
305
|
- spec/unit/lib/yaoc/mapper_registry_spec.rb
|
299
306
|
- spec/unit/lib/yaoc/mapping_base_spec.rb
|
300
307
|
- spec/unit/lib/yaoc/mapping_to_class_spec.rb
|
301
308
|
- spec/unit/lib/yaoc/object_mapper_spec.rb
|
309
|
+
- spec/unit/lib/yaoc/one_to_many_mapper_chain_spec.rb
|
302
310
|
- spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb
|
303
311
|
- spec/unit/lib/yaoc/strategies/to_hash_mapping_spec.rb
|
304
312
|
- spec/unit/lib/yaoc/transformation_command_spec.rb
|