yaoc 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +5 -1
  5. data/Guardfile +5 -0
  6. data/Rakefile +3 -0
  7. data/examples/01_hash_enabled_constructors.rb +8 -8
  8. data/examples/02_procs_as_constructors.rb +11 -13
  9. data/examples/03_positional_constructors.rb +7 -13
  10. data/examples/04_compositions.rb +3 -8
  11. data/examples/05_fill_existing_objects.rb +2 -8
  12. data/examples/06_lazy_loading.rb +0 -7
  13. data/examples/all_examples.rb +2 -2
  14. data/lib/yaoc.rb +2 -3
  15. data/lib/yaoc/converter_builder.rb +14 -21
  16. data/lib/yaoc/helper/struct_hash_constructor.rb +3 -4
  17. data/lib/yaoc/helper/to_proc_delegator.rb +4 -6
  18. data/lib/yaoc/many_to_one_mapper_chain.rb +11 -13
  19. data/lib/yaoc/mapper_registry.rb +3 -4
  20. data/lib/yaoc/mapping_base.rb +11 -12
  21. data/lib/yaoc/mapping_to_class.rb +7 -9
  22. data/lib/yaoc/object_mapper.rb +10 -13
  23. data/lib/yaoc/one_to_many_mapper_chain.rb +4 -6
  24. data/lib/yaoc/strategies/to_array_mapping.rb +1 -3
  25. data/lib/yaoc/strategies/to_hash_mapping.rb +1 -3
  26. data/lib/yaoc/transformation_command.rb +27 -8
  27. data/lib/yaoc/transformation_deferred_command.rb +3 -5
  28. data/lib/yaoc/version.rb +1 -1
  29. data/rubocop-todo.yml +96 -0
  30. data/spec/acceptance/fill_existing_objects_spec.rb +30 -30
  31. data/spec/acceptance/map_multiple_objects_to_one_in_a_chain_spec.rb +15 -15
  32. data/spec/acceptance/map_objects_spec.rb +22 -22
  33. data/spec/acceptance/map_one_object_to_many_in_a_chain_spec.rb +15 -15
  34. data/spec/acceptance/map_to_objects_using_other_converters_spec.rb +53 -55
  35. data/spec/acceptance/map_to_objects_with_lazy_loading_spec.rb +17 -17
  36. data/spec/acceptance/map_to_objects_with_positional_constructors_spec.rb +19 -19
  37. data/spec/integration/lib/yaoc/converter_builder_spec.rb +21 -21
  38. data/spec/spec_helper.rb +7 -7
  39. data/spec/support/feature.rb +1 -2
  40. data/spec/unit/lib/yaoc/converter_builder_spec.rb +42 -44
  41. data/spec/unit/lib/yaoc/helper/struct_hash_constructor_spec.rb +15 -15
  42. data/spec/unit/lib/yaoc/helper/to_proc_delegator_spec.rb +11 -11
  43. data/spec/unit/lib/yaoc/many_to_one_mapper_chain_spec.rb +19 -20
  44. data/spec/unit/lib/yaoc/mapper_registry_spec.rb +6 -6
  45. data/spec/unit/lib/yaoc/mapping_base_spec.rb +36 -36
  46. data/spec/unit/lib/yaoc/mapping_to_class_spec.rb +19 -21
  47. data/spec/unit/lib/yaoc/object_mapper_spec.rb +45 -49
  48. data/spec/unit/lib/yaoc/one_to_many_mapper_chain_spec.rb +17 -18
  49. data/spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb +23 -24
  50. data/spec/unit/lib/yaoc/strategies/to_hash_mapping_spec.rb +25 -25
  51. data/spec/unit/lib/yaoc/transformation_command_spec.rb +13 -13
  52. data/spec/unit/lib/yaoc/transformation_deferred_command_spec.rb +9 -9
  53. data/yaoc.gemspec +1 -0
  54. metadata +18 -2
@@ -1,7 +1,7 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  describe Yaoc::MappingToClass do
4
- subject{
4
+ subject do
5
5
  Struct.new(:target_source) do
6
6
  include Yaoc::MappingToClass
7
7
 
@@ -10,19 +10,19 @@ describe Yaoc::MappingToClass do
10
10
  }
11
11
 
12
12
  end.new(expected_class)
13
- }
13
+ end
14
14
 
15
- let(:expected_class){
15
+ let(:expected_class)do
16
16
  Struct.new(:id)
17
- }
17
+ end
18
18
 
19
- describe "#call" do
20
- subject{
19
+ describe '#call' do
20
+ subject do
21
21
  Struct.new(:target_source) do
22
22
  include Yaoc::MappingToClass
23
23
 
24
24
  self.mapping_strategy = ->(obj){
25
- {:name => :new_name}
25
+ { name: :new_name }
26
26
  }
27
27
 
28
28
  def to_convert
@@ -30,22 +30,21 @@ describe Yaoc::MappingToClass do
30
30
  end
31
31
 
32
32
  end.new(expected_class)
33
- }
33
+ end
34
34
 
35
- it "creates on object of the wanted kind" do
35
+ it 'creates on object of the wanted kind' do
36
36
  expect(subject.call).to be_kind_of expected_class
37
37
  end
38
38
 
39
- it "can use a lambda for creation" do
40
- creator = ->(*args){}
39
+ it 'can use a lambda for creation' do
40
+ creator = ->(*args) {}
41
41
  expect(creator).to receive :call
42
42
  subject.target_source = creator
43
43
  subject.call
44
44
  end
45
45
 
46
-
47
- it "splattes args when conversion result is an array" do
48
- creator = ->(*args){}
46
+ it 'splattes args when conversion result is an array' do
47
+ creator = ->(*args) {}
49
48
  subject.class.mapping_strategy = ->(obj){
50
49
  [1, 2]
51
50
  }
@@ -57,7 +56,7 @@ describe Yaoc::MappingToClass do
57
56
  subject.call
58
57
  end
59
58
 
60
- it "fills an existing object instead of create a new one" do
59
+ it 'fills an existing object instead of create a new one' do
61
60
  obj = Struct.new(:id, :name).new(:my_id)
62
61
  created_obj = subject.call(obj)
63
62
 
@@ -66,17 +65,16 @@ describe Yaoc::MappingToClass do
66
65
  expect(obj.id).to eq :my_id
67
66
  end
68
67
 
69
- it "returns nil when nothing to convert" do
68
+ it 'returns nil when nothing to convert' do
70
69
  subject.stub(to_convert: nil)
71
70
  expect(subject.call).to be_nil
72
71
  end
73
72
  end
74
73
 
75
- describe "#to_a" do
76
- it "satisfies Array(*) when included into structs" do
74
+ describe '#to_a' do
75
+ it 'satisfies Array(*) when included into structs' do
77
76
  expect(subject.to_a).to eq ([subject])
78
77
  end
79
78
  end
80
79
 
81
-
82
- end
80
+ end
@@ -1,30 +1,30 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  describe Yaoc::ObjectMapper do
4
- subject{
5
- Yaoc::ObjectMapper.new(Struct.new(:id, :name)).tap{|mapper|
4
+ subject do
5
+ Yaoc::ObjectMapper.new(Struct.new(:id, :name)).tap do|mapper|
6
6
  mapper.stub(:converter_builder).and_return(converter_builder)
7
7
  mapper.stub(:reverse_converter_builder).and_return(reverse_converter_builder)
8
- }
9
- }
8
+ end
9
+ end
10
10
 
11
- let(:converter_builder){
12
- double("converter_builder", rule: nil, apply_commands!: nil, converter: converter)
13
- }
11
+ let(:converter_builder)do
12
+ double('converter_builder', rule: nil, apply_commands!: nil, converter: converter)
13
+ end
14
14
 
15
- let(:reverse_converter_builder){
16
- double("reverse_converter_builder", rule: nil, apply_commands!: nil, converter: reverse_converter)
17
- }
15
+ let(:reverse_converter_builder)do
16
+ double('reverse_converter_builder', rule: nil, apply_commands!: nil, converter: reverse_converter)
17
+ end
18
18
 
19
- let(:converter){
20
- double("converter", call: nil)
21
- }
19
+ let(:converter)do
20
+ double('converter', call: nil)
21
+ end
22
22
 
23
- let(:reverse_converter){
24
- double("reverse_converter", call: nil)
25
- }
23
+ let(:reverse_converter)do
24
+ double('reverse_converter', call: nil)
25
+ end
26
26
 
27
- let(:expected_default_params){
27
+ let(:expected_default_params)do
28
28
  {
29
29
  to: :id,
30
30
  from: :id,
@@ -33,11 +33,11 @@ describe Yaoc::ObjectMapper do
33
33
  is_collection: nil,
34
34
  lazy_loading: nil,
35
35
  }
36
- }
36
+ end
37
37
 
38
- describe "#add_mapping" do
38
+ describe '#add_mapping' do
39
39
 
40
- it "creates a converter" do
40
+ it 'creates a converter' do
41
41
  expected_params = expected_default_params
42
42
 
43
43
  expect(converter_builder).to receive(:rule).with(expected_params)
@@ -48,7 +48,7 @@ describe Yaoc::ObjectMapper do
48
48
 
49
49
  end
50
50
 
51
- it "creates a revers converter" do
51
+ it 'creates a revers converter' do
52
52
  expected_params = expected_default_params
53
53
 
54
54
  expect(reverse_converter_builder).to receive(:rule).with(expected_params)
@@ -61,10 +61,10 @@ describe Yaoc::ObjectMapper do
61
61
 
62
62
  end
63
63
 
64
- describe "#rule" do
64
+ describe '#rule' do
65
65
 
66
- it "allows to use another converter as converter" do
67
- converter_double = double("converter")
66
+ it 'allows to use another converter as converter' do
67
+ converter_double = double('converter')
68
68
 
69
69
  expected_params = expected_default_params.merge(
70
70
  object_converter: [converter_double],
@@ -74,22 +74,20 @@ describe Yaoc::ObjectMapper do
74
74
  object_converter: [converter_double]
75
75
  )
76
76
 
77
-
78
77
  expect(converter_builder).to receive(:rule).with(expected_params)
79
78
  expect(reverse_converter_builder).to receive(:rule).with(expected_params_reverse)
80
79
 
81
80
  expect(converter_double).to receive(:converter).and_return(converter_double)
82
81
  expect(converter_double).to receive(:reverse_converter).and_return(converter_double)
83
82
 
84
-
85
83
  subject.add_mapping do
86
84
  rule to: :id,
87
85
  object_converter: converter_double
88
86
  end
89
87
  end
90
88
 
91
- it "allows to use another converter as reverse converter" do
92
- reverse_converter_double = double("reverse converter")
89
+ it 'allows to use another converter as reverse converter' do
90
+ reverse_converter_double = double('reverse converter')
93
91
 
94
92
  expected_params = expected_default_params.merge(
95
93
  object_converter: [],
@@ -99,7 +97,6 @@ describe Yaoc::ObjectMapper do
99
97
  object_converter: [reverse_converter_double]
100
98
  )
101
99
 
102
-
103
100
  expect(converter_builder).to receive(:rule).with(expected_params)
104
101
  expect(reverse_converter_builder).to receive(:rule).with(expected_params_reverse)
105
102
 
@@ -111,7 +108,7 @@ describe Yaoc::ObjectMapper do
111
108
  end
112
109
  end
113
110
 
114
- it "accepts a reverse mapping for from and to" do
111
+ it 'accepts a reverse mapping for from and to' do
115
112
  expected_params = expected_default_params.merge(
116
113
  to: :id_r,
117
114
  from: :id_r,
@@ -124,7 +121,7 @@ describe Yaoc::ObjectMapper do
124
121
  end
125
122
  end
126
123
 
127
- it "allows to set a fetcher" do
124
+ it 'allows to set a fetcher' do
128
125
  expect(converter_builder).to receive(:fetcher=).with(:public_send)
129
126
 
130
127
  subject.add_mapping do
@@ -134,7 +131,7 @@ describe Yaoc::ObjectMapper do
134
131
 
135
132
  end
136
133
 
137
- it "allows to set a reverse_fetcher" do
134
+ it 'allows to set a reverse_fetcher' do
138
135
  expect(reverse_converter_builder).to receive(:fetcher=).with(:fetch)
139
136
 
140
137
  subject.add_mapping do
@@ -143,7 +140,7 @@ describe Yaoc::ObjectMapper do
143
140
  end
144
141
  end
145
142
 
146
- it "allows to change the strategy" do
143
+ it 'allows to change the strategy' do
147
144
  expect(converter_builder).to receive(:strategy=).with(:to_array_mapping)
148
145
 
149
146
  subject.add_mapping do
@@ -152,7 +149,7 @@ describe Yaoc::ObjectMapper do
152
149
  end
153
150
  end
154
151
 
155
- it "allows to change the reverse strategy" do
152
+ it 'allows to change the reverse strategy' do
156
153
  expect(reverse_converter_builder).to receive(:strategy=).with(:to_array_mapping)
157
154
 
158
155
  subject.add_mapping do
@@ -161,7 +158,7 @@ describe Yaoc::ObjectMapper do
161
158
  end
162
159
  end
163
160
 
164
- it "allows to set lazy_loading" do
161
+ it 'allows to set lazy_loading' do
165
162
  expected_params = expected_default_params.merge(
166
163
  lazy_loading: true,
167
164
  )
@@ -175,7 +172,7 @@ describe Yaoc::ObjectMapper do
175
172
  end
176
173
  end
177
174
 
178
- it "allows to set reverse lazy_loading" do
175
+ it 'allows to set reverse lazy_loading' do
179
176
  expected_params = expected_default_params.merge(
180
177
  lazy_loading: nil,
181
178
  )
@@ -194,7 +191,7 @@ describe Yaoc::ObjectMapper do
194
191
  end
195
192
 
196
193
  it 'allows to register the mapper globally' do
197
- registry_double=double('registry')
194
+ registry_double = double('registry')
198
195
  subject.registry = registry_double
199
196
 
200
197
  expect(registry_double).to receive(:add).with(:mapper_name, subject)
@@ -205,17 +202,16 @@ describe Yaoc::ObjectMapper do
205
202
 
206
203
  end
207
204
 
208
-
209
205
  end
210
206
 
211
- describe "#load" do
212
- it "creates an object of result class kind" do
207
+ describe '#load' do
208
+ it 'creates an object of result class kind' do
213
209
  expect(converter).to receive(:call)
214
210
 
215
211
  subject.load({})
216
212
  end
217
213
 
218
- it "uses an existing object for the result" do
214
+ it 'uses an existing object for the result' do
219
215
  preloaded_obj = Object.new
220
216
 
221
217
  expect(converter).to receive(:call).with(preloaded_obj)
@@ -224,15 +220,15 @@ describe Yaoc::ObjectMapper do
224
220
  end
225
221
  end
226
222
 
227
- describe "#dump" do
223
+ describe '#dump' do
228
224
 
229
- it "dump the object as an wanted object" do
225
+ it 'dump the object as an wanted object' do
230
226
  expect(reverse_converter).to receive(:call)
231
227
 
232
228
  subject.dump({})
233
229
  end
234
230
 
235
- it "uses an existing object for the result" do
231
+ it 'uses an existing object for the result' do
236
232
  preloaded_obj = Object.new
237
233
 
238
234
  expect(reverse_converter).to receive(:call).with(preloaded_obj)
@@ -242,8 +238,8 @@ describe Yaoc::ObjectMapper do
242
238
 
243
239
  end
244
240
 
245
- describe "#noop" do
246
- it "returns the input" do
241
+ describe '#noop' do
242
+ it 'returns the input' do
247
243
  expect(subject.noop.call(:some_thing, :expected_value)).to eq :expected_value
248
244
  end
249
245
  end
@@ -252,8 +248,8 @@ describe Yaoc::ObjectMapper do
252
248
  it 'returns a readable representation' do
253
249
  subject.dump_result_source = Object
254
250
  subject.load_result_source = nil
255
- expect(subject.to_s).to eq "Object <=> "
251
+ expect(subject.to_s).to eq 'Object <=> '
256
252
  end
257
253
  end
258
254
 
259
- end
255
+ end
@@ -2,11 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe Yaoc::OneToManyMapperChain do
4
4
 
5
- subject{
5
+ subject do
6
6
  Yaoc::OneToManyMapperChain.new(first_mapper, second_mapper)
7
- }
7
+ end
8
8
 
9
- let(:first_mapper){
9
+ let(:first_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,9 +14,9 @@ describe Yaoc::OneToManyMapperChain do
14
14
  from: :o_id
15
15
  end
16
16
  end
17
- }
17
+ end
18
18
 
19
- let(:second_mapper){
19
+ let(:second_mapper)do
20
20
  Yaoc::ObjectMapper.new(new_user_class, old_user_class).tap do |mapper|
21
21
  mapper.add_mapping do
22
22
  fetcher :public_send
@@ -24,34 +24,34 @@ describe Yaoc::OneToManyMapperChain do
24
24
  from: :o_names
25
25
  end
26
26
  end
27
- }
27
+ end
28
28
 
29
- let(:new_user_class){
29
+ let(:new_user_class)do
30
30
  Yaoc::Helper::StructHE(:id, :names)
31
- }
31
+ end
32
32
 
33
- let(:old_user_class){
33
+ let(:old_user_class)do
34
34
  Yaoc::Helper::StructHE(:o_id, :o_names)
35
- }
35
+ end
36
36
 
37
- let(:existing_old_user){
37
+ let(:existing_old_user)do
38
38
  old_user_class.new(
39
39
  o_id: 'existing_user_2',
40
40
  o_names: ['first_name', 'second_name']
41
41
  )
42
- }
42
+ end
43
43
 
44
- let(:existing_user){
44
+ let(:existing_user)do
45
45
  new_user_class.new(
46
46
  id: 'existing_user_2',
47
47
  names: ['first_name', 'second_name']
48
48
  )
49
- }
49
+ end
50
50
 
51
51
  describe '.new' do
52
- subject{
52
+ subject do
53
53
  Yaoc::ManyToOneMapperChain
54
- }
54
+ end
55
55
 
56
56
  it 'converts symbols into converter' do
57
57
  registry = double('registry')
@@ -88,7 +88,6 @@ describe Yaoc::OneToManyMapperChain do
88
88
  end
89
89
  end
90
90
 
91
-
92
91
  describe '#dump_all' do
93
92
  it 'converts one input object into multiple result object' do
94
93
  converted_users = subject.dump_all(existing_user)
@@ -110,4 +109,4 @@ describe Yaoc::OneToManyMapperChain do
110
109
  end
111
110
  end
112
111
 
113
- end
112
+ end
@@ -1,38 +1,37 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  describe Yaoc::Strategies::ToArrayMapping do
4
- subject{
4
+ subject do
5
5
  Struct.new(:to_convert) do
6
6
  include Yaoc::MappingBase
7
7
  self.mapping_strategy = Yaoc::Strategies::ToArrayMapping
8
8
  end
9
- }
9
+ end
10
10
 
11
- let(:mapper){
11
+ let(:mapper)do
12
12
  subject.new(source_object)
13
- }
13
+ end
14
14
 
15
- let(:source_object){
16
- {id: 1, name: "paul"}
17
- }
15
+ let(:source_object)do
16
+ { id: 1, name: 'paul' }
17
+ end
18
18
 
19
- let(:expected_array){
20
- [1, "paul"]
21
- }
19
+ let(:expected_array)do
20
+ [1, 'paul']
21
+ end
22
22
 
23
- describe ".call" do
23
+ describe '.call' do
24
24
 
25
- it "creates a hash from a object" do
25
+ it 'creates a hash from a object' do
26
26
  subject.map(to: 0, from: :id)
27
27
  subject.map(to: 1, from: :name)
28
28
 
29
29
  expect(mapper.call).to eq(expected_array)
30
30
  end
31
31
 
32
-
33
- it "uses my converter proc" do
32
+ it 'uses my converter proc' do
34
33
  subject.map(to: 0, from: :id)
35
- subject.map(to: 3, from: :fullname, converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, 3, "#{source.fetch(:name)} Hello World") })
34
+ subject.map(to: 3, from: :fullname, converter: ->(source, result) { Yaoc::TransformationCommand.fill_result_with_value(result, 3, "#{source.fetch(:name)} Hello World") })
36
35
 
37
36
  ext_expectation = expected_array.clone
38
37
  ext_expectation[3] = "#{ext_expectation[1]} Hello World"
@@ -42,12 +41,12 @@ describe Yaoc::Strategies::ToArrayMapping do
42
41
  expect(mapper.call).to eq(ext_expectation)
43
42
  end
44
43
 
45
- context "changed fetcher method" do
46
- let(:source_object){
47
- Struct.new(:id, :name).new(1, "paul")
48
- }
44
+ context 'changed fetcher method' do
45
+ let(:source_object)do
46
+ Struct.new(:id, :name).new(1, 'paul')
47
+ end
49
48
 
50
- it "uses custom fetcher methods" do
49
+ it 'uses custom fetcher methods' do
51
50
  subject.map(to: 0, from: :id)
52
51
  subject.map(to: 1, from: :name)
53
52
 
@@ -58,7 +57,7 @@ describe Yaoc::Strategies::ToArrayMapping do
58
57
  expect(mapper.call).to eq(expected_array)
59
58
  end
60
59
 
61
- it "works with arrays" do
60
+ it 'works with arrays' do
62
61
  subject.map(to: 1, from: 0)
63
62
  subject.map(to: 0, from: 1)
64
63
 
@@ -66,7 +65,7 @@ describe Yaoc::Strategies::ToArrayMapping do
66
65
  :[]
67
66
  end
68
67
 
69
- mapper.to_convert = [1, "paul"]
68
+ mapper.to_convert = [1, 'paul']
70
69
 
71
70
  expect(mapper.call).to eq(expected_array.reverse)
72
71
  end
@@ -74,4 +73,4 @@ describe Yaoc::Strategies::ToArrayMapping do
74
73
 
75
74
  end
76
75
 
77
- end
76
+ end