yaoc 0.0.13 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +5 -1
- data/Guardfile +5 -0
- data/Rakefile +3 -0
- data/examples/01_hash_enabled_constructors.rb +8 -8
- data/examples/02_procs_as_constructors.rb +11 -13
- data/examples/03_positional_constructors.rb +7 -13
- data/examples/04_compositions.rb +3 -8
- data/examples/05_fill_existing_objects.rb +2 -8
- data/examples/06_lazy_loading.rb +0 -7
- data/examples/all_examples.rb +2 -2
- data/lib/yaoc.rb +2 -3
- data/lib/yaoc/converter_builder.rb +14 -21
- data/lib/yaoc/helper/struct_hash_constructor.rb +3 -4
- data/lib/yaoc/helper/to_proc_delegator.rb +4 -6
- data/lib/yaoc/many_to_one_mapper_chain.rb +11 -13
- data/lib/yaoc/mapper_registry.rb +3 -4
- data/lib/yaoc/mapping_base.rb +11 -12
- data/lib/yaoc/mapping_to_class.rb +7 -9
- data/lib/yaoc/object_mapper.rb +10 -13
- data/lib/yaoc/one_to_many_mapper_chain.rb +4 -6
- data/lib/yaoc/strategies/to_array_mapping.rb +1 -3
- data/lib/yaoc/strategies/to_hash_mapping.rb +1 -3
- data/lib/yaoc/transformation_command.rb +27 -8
- data/lib/yaoc/transformation_deferred_command.rb +3 -5
- data/lib/yaoc/version.rb +1 -1
- data/rubocop-todo.yml +96 -0
- data/spec/acceptance/fill_existing_objects_spec.rb +30 -30
- data/spec/acceptance/map_multiple_objects_to_one_in_a_chain_spec.rb +15 -15
- data/spec/acceptance/map_objects_spec.rb +22 -22
- data/spec/acceptance/map_one_object_to_many_in_a_chain_spec.rb +15 -15
- data/spec/acceptance/map_to_objects_using_other_converters_spec.rb +53 -55
- data/spec/acceptance/map_to_objects_with_lazy_loading_spec.rb +17 -17
- data/spec/acceptance/map_to_objects_with_positional_constructors_spec.rb +19 -19
- data/spec/integration/lib/yaoc/converter_builder_spec.rb +21 -21
- data/spec/spec_helper.rb +7 -7
- data/spec/support/feature.rb +1 -2
- data/spec/unit/lib/yaoc/converter_builder_spec.rb +42 -44
- data/spec/unit/lib/yaoc/helper/struct_hash_constructor_spec.rb +15 -15
- data/spec/unit/lib/yaoc/helper/to_proc_delegator_spec.rb +11 -11
- data/spec/unit/lib/yaoc/many_to_one_mapper_chain_spec.rb +19 -20
- data/spec/unit/lib/yaoc/mapper_registry_spec.rb +6 -6
- data/spec/unit/lib/yaoc/mapping_base_spec.rb +36 -36
- data/spec/unit/lib/yaoc/mapping_to_class_spec.rb +19 -21
- data/spec/unit/lib/yaoc/object_mapper_spec.rb +45 -49
- data/spec/unit/lib/yaoc/one_to_many_mapper_chain_spec.rb +17 -18
- data/spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb +23 -24
- data/spec/unit/lib/yaoc/strategies/to_hash_mapping_spec.rb +25 -25
- data/spec/unit/lib/yaoc/transformation_command_spec.rb +13 -13
- data/spec/unit/lib/yaoc/transformation_deferred_command_spec.rb +9 -9
- data/yaoc.gemspec +1 -0
- metadata +18 -2
@@ -1,21 +1,20 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
feature
|
3
|
+
feature 'Map objects reusing other existing converters', %q{
|
4
4
|
In order to map objects with other converters
|
5
5
|
as a lib user
|
6
6
|
I want to map object from an input object to an output object and reverse with a given converter
|
7
7
|
} do
|
8
8
|
|
9
|
-
|
10
|
-
given(:new_role_class){
|
9
|
+
given(:new_role_class)do
|
11
10
|
Yaoc::Helper::StructHE(:id, :name)
|
12
|
-
|
11
|
+
end
|
13
12
|
|
14
|
-
given(:old_role_class)
|
13
|
+
given(:old_role_class)do
|
15
14
|
Yaoc::Helper::StructHE(:o_id, :o_name)
|
16
|
-
|
15
|
+
end
|
17
16
|
|
18
|
-
given(:role_converter)
|
17
|
+
given(:role_converter)do
|
19
18
|
Yaoc::ObjectMapper.new(new_role_class, old_role_class).tap do |mapper|
|
20
19
|
mapper.add_mapping do
|
21
20
|
fetcher :public_send
|
@@ -25,17 +24,17 @@ feature "Map objects reusing other existing converters", %q{
|
|
25
24
|
|
26
25
|
end
|
27
26
|
end
|
28
|
-
|
27
|
+
end
|
29
28
|
|
30
|
-
given(:new_user_class)
|
29
|
+
given(:new_user_class)do
|
31
30
|
Yaoc::Helper::StructHE(:id, :firstname, :lastname, :roles)
|
32
|
-
|
31
|
+
end
|
33
32
|
|
34
|
-
given(:old_user_class)
|
33
|
+
given(:old_user_class)do
|
35
34
|
Yaoc::Helper::StructHE(:o_id, :o_firstname, :o_lastname, :o_roles)
|
36
|
-
|
35
|
+
end
|
37
36
|
|
38
|
-
given(:user_converter)
|
37
|
+
given(:user_converter)do
|
39
38
|
other_converter = role_converter
|
40
39
|
is_col = is_collection
|
41
40
|
|
@@ -53,82 +52,81 @@ feature "Map objects reusing other existing converters", %q{
|
|
53
52
|
is_collection: is_col
|
54
53
|
end
|
55
54
|
end
|
56
|
-
|
57
|
-
|
55
|
+
end
|
58
56
|
|
59
|
-
context
|
60
|
-
given(:is_collection)
|
57
|
+
context 'composition is a collection' do
|
58
|
+
given(:is_collection)do
|
61
59
|
true
|
62
|
-
|
60
|
+
end
|
63
61
|
|
64
|
-
given(:old_user)
|
62
|
+
given(:old_user) do
|
65
63
|
old_user_class.new(
|
66
|
-
o_id:
|
67
|
-
o_firstname:
|
68
|
-
o_lastname:
|
64
|
+
o_id: 'user_1',
|
65
|
+
o_firstname: 'o firstname',
|
66
|
+
o_lastname: 'o lastname',
|
69
67
|
o_roles: [
|
70
|
-
old_role_class.new(o_id:
|
71
|
-
old_role_class.new(o_id:
|
72
|
-
old_role_class.new(o_id:
|
68
|
+
old_role_class.new(o_id: 'role_1', o_name: 'admin'),
|
69
|
+
old_role_class.new(o_id: 'role_2', o_name: 'ruth'),
|
70
|
+
old_role_class.new(o_id: 'role_3', o_name: 'guest'),
|
73
71
|
]
|
74
72
|
)
|
75
|
-
|
73
|
+
end
|
76
74
|
|
77
|
-
given(:expected_new_user)
|
75
|
+
given(:expected_new_user) do
|
78
76
|
new_user_class.new(
|
79
|
-
id:
|
80
|
-
firstname:
|
81
|
-
lastname:
|
77
|
+
id: 'user_1',
|
78
|
+
firstname: 'o firstname',
|
79
|
+
lastname: 'o lastname',
|
82
80
|
roles: [
|
83
|
-
new_role_class.new(id:
|
84
|
-
new_role_class.new(id:
|
85
|
-
new_role_class.new(id:
|
81
|
+
new_role_class.new(id: 'role_1', name: 'admin'),
|
82
|
+
new_role_class.new(id: 'role_2', name: 'ruth'),
|
83
|
+
new_role_class.new(id: 'role_3', name: 'guest'),
|
86
84
|
]
|
87
85
|
)
|
88
|
-
|
86
|
+
end
|
89
87
|
|
90
|
-
scenario
|
88
|
+
scenario 'creates a new user from the old one' do
|
91
89
|
expect(user_converter.load(old_user)).to eq expected_new_user
|
92
90
|
end
|
93
91
|
|
94
|
-
scenario
|
92
|
+
scenario 'dumps an result object as result object' do
|
95
93
|
expect(user_converter.dump(expected_new_user)).to eq old_user
|
96
94
|
end
|
97
95
|
|
98
96
|
end
|
99
97
|
|
100
|
-
context
|
101
|
-
given(:is_collection)
|
98
|
+
context 'composition is a single value' do
|
99
|
+
given(:is_collection)do
|
102
100
|
false
|
103
|
-
|
101
|
+
end
|
104
102
|
|
105
|
-
given(:old_user)
|
103
|
+
given(:old_user) do
|
106
104
|
old_user_class.new(
|
107
|
-
o_id:
|
108
|
-
o_firstname:
|
109
|
-
o_lastname:
|
110
|
-
o_roles: old_role_class.new(o_id:
|
105
|
+
o_id: 'user_1',
|
106
|
+
o_firstname: 'o firstname',
|
107
|
+
o_lastname: 'o lastname',
|
108
|
+
o_roles: old_role_class.new(o_id: 'role_1', o_name: 'admin, ruth, guest')
|
111
109
|
|
112
110
|
)
|
113
|
-
|
111
|
+
end
|
114
112
|
|
115
|
-
given(:expected_new_user)
|
113
|
+
given(:expected_new_user) do
|
116
114
|
new_user_class.new(
|
117
|
-
id:
|
118
|
-
firstname:
|
119
|
-
lastname:
|
120
|
-
roles: new_role_class.new(id:
|
115
|
+
id: 'user_1',
|
116
|
+
firstname: 'o firstname',
|
117
|
+
lastname: 'o lastname',
|
118
|
+
roles: new_role_class.new(id: 'role_1', name: 'admin, ruth, guest')
|
121
119
|
)
|
122
|
-
|
120
|
+
end
|
123
121
|
|
124
|
-
scenario
|
122
|
+
scenario 'creates a new user from the old one' do
|
125
123
|
expect(user_converter.load(old_user)).to eq expected_new_user
|
126
124
|
end
|
127
125
|
|
128
|
-
scenario
|
126
|
+
scenario 'dumps an result object as result object' do
|
129
127
|
expect(user_converter.dump(expected_new_user)).to eq old_user
|
130
128
|
end
|
131
129
|
|
132
130
|
end
|
133
131
|
|
134
|
-
end
|
132
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
feature
|
3
|
+
feature 'Map objects to classes with lazy loading', %q{
|
4
4
|
In order to defer object mapping
|
5
5
|
as a lib user
|
6
6
|
I want to map object from an input object to an output object with lazy loading support
|
7
7
|
} do
|
8
8
|
|
9
|
-
given(:mapper)
|
9
|
+
given(:mapper)do
|
10
10
|
Yaoc::ObjectMapper.new(new_user_class, old_user_class).tap do |mapper|
|
11
11
|
mapper.add_mapping do
|
12
12
|
fetcher :public_send
|
@@ -14,46 +14,46 @@ feature "Map objects to classes with lazy loading", %q{
|
|
14
14
|
lazy_loading: [false, true]
|
15
15
|
end
|
16
16
|
end
|
17
|
-
|
17
|
+
end
|
18
18
|
|
19
|
-
given(:new_user_class)
|
19
|
+
given(:new_user_class)do
|
20
20
|
Yaoc::Helper::StructHE(:id, :names)
|
21
|
-
|
21
|
+
end
|
22
22
|
|
23
|
-
given(:old_user_class)
|
23
|
+
given(:old_user_class)do
|
24
24
|
Yaoc::Helper::StructHE(:id, :names)
|
25
|
-
|
25
|
+
end
|
26
26
|
|
27
|
-
given(:existing_old_user)
|
27
|
+
given(:existing_old_user)do
|
28
28
|
old_user_class.new(
|
29
29
|
id: 'existing_user_2',
|
30
30
|
names: ['first_name', 'second_name']
|
31
31
|
)
|
32
|
-
|
32
|
+
end
|
33
33
|
|
34
|
-
given(:existing_user)
|
34
|
+
given(:existing_user)do
|
35
35
|
new_user_class.new(
|
36
36
|
id: 'existing_user_2',
|
37
37
|
names: ['first_name', 'second_name']
|
38
38
|
)
|
39
|
-
|
39
|
+
end
|
40
40
|
|
41
|
-
scenario
|
41
|
+
scenario 'creates an result object from an input_object deferred' do
|
42
42
|
converted_user = mapper.load(existing_old_user)
|
43
|
-
new_names = [
|
43
|
+
new_names = ['new_name1', 'new_name2']
|
44
44
|
|
45
45
|
existing_old_user.names = new_names # show defer through changes after loading an object
|
46
46
|
|
47
47
|
expect(converted_user.names).to eq new_names
|
48
48
|
end
|
49
49
|
|
50
|
-
scenario
|
50
|
+
scenario 'dumps an result object as source object defered' do
|
51
51
|
converted_user = mapper.dump(existing_user)
|
52
|
-
new_names = [
|
52
|
+
new_names = ['new_name1', 'new_name2']
|
53
53
|
|
54
54
|
existing_user.names = new_names # show defer through changes after loading an object
|
55
55
|
|
56
56
|
expect(converted_user.names).to eq new_names
|
57
57
|
end
|
58
58
|
|
59
|
-
end
|
59
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
feature
|
3
|
+
feature 'Map objects to classes with positional constructors', %q{
|
4
4
|
In order to map objects with positional constructors
|
5
5
|
as a lib user
|
6
6
|
I want to map object from an input object to an output object and reverse with a given mapping strategy
|
7
7
|
} do
|
8
8
|
|
9
|
-
given(:mapper)
|
9
|
+
given(:mapper)do
|
10
10
|
Yaoc::ObjectMapper.new(load_result_object_class, dump_result_object_class).tap do |mapper|
|
11
11
|
mapper.add_mapping do
|
12
12
|
fetcher :public_send
|
@@ -21,38 +21,38 @@ feature "Map objects to classes with positional constructors", %q{
|
|
21
21
|
reverse_from: :name
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
24
|
+
end
|
25
25
|
|
26
|
-
given(:load_result_object_class)
|
26
|
+
given(:load_result_object_class) do
|
27
27
|
Struct.new(:id, :name) do
|
28
28
|
include Equalizer.new(:id, :name)
|
29
29
|
end
|
30
|
-
|
30
|
+
end
|
31
31
|
|
32
|
-
given(:dump_result_object_class)
|
32
|
+
given(:dump_result_object_class) do
|
33
33
|
Yaoc::Helper::StructH(:id, :name) do
|
34
34
|
include Equalizer.new(:id, :name)
|
35
35
|
end
|
36
|
-
|
36
|
+
end
|
37
37
|
|
38
|
-
given(:input_object)
|
38
|
+
given(:input_object)do
|
39
39
|
dump_result_object
|
40
|
-
|
40
|
+
end
|
41
41
|
|
42
|
-
given(:load_result_object)
|
43
|
-
load_result_object_class.new(1,
|
44
|
-
|
42
|
+
given(:load_result_object)do
|
43
|
+
load_result_object_class.new(1,'paul')
|
44
|
+
end
|
45
45
|
|
46
|
-
given(:dump_result_object)
|
47
|
-
dump_result_object_class.new(
|
48
|
-
|
46
|
+
given(:dump_result_object)do
|
47
|
+
dump_result_object_class.new(id: 1, name: 'paul')
|
48
|
+
end
|
49
49
|
|
50
|
-
scenario
|
50
|
+
scenario 'creates an result object from an input_object' do
|
51
51
|
expect(mapper.load(input_object)).to eq load_result_object
|
52
52
|
end
|
53
53
|
|
54
|
-
scenario
|
54
|
+
scenario 'dumps an result object as result object' do
|
55
55
|
expect(mapper.dump(load_result_object)).to eq dump_result_object
|
56
56
|
end
|
57
57
|
|
58
|
-
end
|
58
|
+
end
|
@@ -1,11 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Yaoc::ConverterBuilder do
|
4
|
-
subject
|
4
|
+
subject do
|
5
5
|
ot = other_converter
|
6
6
|
is_col = is_collection
|
7
7
|
|
8
|
-
Yaoc::ConverterBuilder.new
|
8
|
+
Yaoc::ConverterBuilder.new.tap do|converter|
|
9
9
|
converter.add_mapping do
|
10
10
|
fetch_with :[]
|
11
11
|
rule to: :id,
|
@@ -13,16 +13,16 @@ describe Yaoc::ConverterBuilder do
|
|
13
13
|
is_collection: is_col,
|
14
14
|
object_converter: ot
|
15
15
|
end
|
16
|
-
|
16
|
+
end
|
17
17
|
|
18
|
-
|
18
|
+
end
|
19
19
|
|
20
|
-
let(:other_converter)
|
21
|
-
Class.new
|
20
|
+
let(:other_converter)do
|
21
|
+
Class.new do
|
22
22
|
def to_proc
|
23
|
-
@proc ||= ->(index, *args)
|
23
|
+
@proc ||= ->(index, *args)do
|
24
24
|
[nil, nil, :my_result_1, nil, :my_result_2][index]
|
25
|
-
|
25
|
+
end
|
26
26
|
end
|
27
27
|
|
28
28
|
def to_a
|
@@ -30,29 +30,29 @@ describe Yaoc::ConverterBuilder do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
end.new
|
33
|
-
|
33
|
+
end
|
34
34
|
|
35
|
-
let(:is_collection)
|
35
|
+
let(:is_collection)do
|
36
36
|
false
|
37
|
-
|
37
|
+
end
|
38
38
|
|
39
|
-
describe
|
39
|
+
describe '#converter_to_proc' do
|
40
40
|
|
41
|
-
it
|
41
|
+
it 'creates a converter proc' do
|
42
42
|
expect(other_converter.to_proc).to receive(:call).with(2).and_return(:my_result)
|
43
|
-
expect(subject.converter(nil, nil).map_0000_name_to_id({:
|
43
|
+
expect(subject.converter(nil, nil).map_0000_name_to_id({ name: 2 },{})).to eq(id: :my_result)
|
44
44
|
end
|
45
45
|
|
46
|
-
context
|
47
|
-
let(:is_collection)
|
46
|
+
context 'value to convert is a collection' do
|
47
|
+
let(:is_collection)do
|
48
48
|
true
|
49
|
-
|
49
|
+
end
|
50
50
|
|
51
|
-
it
|
52
|
-
expect(subject.converter(nil, nil).map_0000_name_to_id({:
|
51
|
+
it 'creates a converter proc for collections' do
|
52
|
+
expect(subject.converter(nil, nil).map_0000_name_to_id({ name: [2, 4] },{})).to eq(id: [:my_result_1, :my_result_2])
|
53
53
|
end
|
54
54
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
end
|
58
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,26 +2,26 @@ require 'bundler/setup'
|
|
2
2
|
Bundler.require(:development)
|
3
3
|
|
4
4
|
require 'coveralls'
|
5
|
-
Coveralls.wear! unless ENV[
|
5
|
+
Coveralls.wear! unless ENV['SIMPLE_COVERAGE']
|
6
6
|
|
7
7
|
begin
|
8
|
-
if ENV[
|
8
|
+
if ENV['SIMPLE_COVERAGE']
|
9
9
|
require 'simplecov'
|
10
10
|
SimpleCov.start do
|
11
|
-
add_group
|
11
|
+
add_group 'Lib', 'lib'
|
12
12
|
|
13
|
-
add_filter
|
13
|
+
add_filter '/spec/'
|
14
14
|
end
|
15
15
|
end
|
16
16
|
rescue LoadError
|
17
|
-
warn
|
17
|
+
warn '=' * 80
|
18
18
|
warn 'simplecov not installed. No coverage report'
|
19
|
-
warn
|
19
|
+
warn '=' * 80
|
20
20
|
end
|
21
21
|
|
22
22
|
require 'yaoc'
|
23
23
|
|
24
|
-
Dir[File.join(File.expand_path(__dir__
|
24
|
+
Dir[File.join(File.expand_path(__dir__), 'support/**/*.rb')].each { |f| require f }
|
25
25
|
|
26
26
|
# This file was generated by the `rspec --init` command. Conventionally, all
|
27
27
|
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
data/spec/support/feature.rb
CHANGED
@@ -16,7 +16,6 @@ module Capybara
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
19
|
def self.feature(*args, &block)
|
21
20
|
options = if args.last.is_a?(Hash) then args.pop else {} end
|
22
21
|
options[:capybara_feature] = true
|
@@ -27,4 +26,4 @@ def self.feature(*args, &block)
|
|
27
26
|
describe(*args, &block)
|
28
27
|
end
|
29
28
|
|
30
|
-
RSpec.configuration.include Capybara::Features, :
|
29
|
+
RSpec.configuration.include Capybara::Features, capybara_feature: true
|