yaoc 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa280d846afba6ccd2f33412e0ff29851e644456
4
- data.tar.gz: 9db2caf9165fafc3225b647d9a7fd504dc302a1b
3
+ metadata.gz: 441263ba3b9fe09627de69b63629026703c7070d
4
+ data.tar.gz: 48c6614551a01d37000e914bd48308aca9987424
5
5
  SHA512:
6
- metadata.gz: 002631bd391b6fe657556f7c2c80c0289fb5ab26cb04e78c8593abc9b1e1288e80d2646ea8b63433d9ca20cb4bed2d4e7a477be0fe26dcce757b8363f5981f51
7
- data.tar.gz: 08ef26547007bd532debdcbd39045de8d61e96adc59d9b812329d2b5e3b4ebeb408df5e3137cc0e76866d8667bb461d89c1b7c728c86da9f2a5a8ab949d2973a
6
+ metadata.gz: d6284f7e096825e82d6244e75eb239a1ef2d388bce4714c71d844619cbd0a71d8923770134947ee81e90a544c67255a9146ff463005794b28f53731f98cc772f
7
+ data.tar.gz: cc8596b471a03029d0edf21f96dec0e47a1df2a5586ed496c331c9b260a7ea39af0aec3193d8d0d0e81f2a7dd658b3547398727b89c0b911c9155d5f86c304b4
data/README.md CHANGED
@@ -24,7 +24,7 @@ Uptodate doc's look into the specs.
24
24
 
25
25
  require 'yaoc'
26
26
 
27
- User = Struct.new(:id, :name) do
27
+ User = Struct.new(:id, :firstname, :lastname, :role) do
28
28
  def initialize(params={})
29
29
  super()
30
30
 
@@ -34,7 +34,7 @@ User = Struct.new(:id, :name) do
34
34
  end
35
35
  end
36
36
 
37
- OldUser = Struct.new(:id, :fullname) do
37
+ OldUser = Struct.new(:id, :fullname, :r_role) do
38
38
  def initialize(params={})
39
39
  super()
40
40
 
@@ -47,25 +47,100 @@ end
47
47
  mapper = Yaoc::ObjectMapper.new(User, OldUser).tap do |mapper|
48
48
  mapper.add_mapping do
49
49
  fetcher :public_send
50
- rule to: :name, from: :fullname
50
+ rule to: :role, from: :r_role
51
+
52
+ rule to: :firstname,
53
+ from: :fullname,
54
+ converter: ->(source, result){ result.merge({firstname:
55
+ source.fullname.split().first }) },
56
+ reverse_converter: ->(source, result){ result.merge({fullname:
57
+ "#{source.firstname} #{source.lastname}" }) }
58
+
59
+ rule to: :lastname,
60
+ from: :fullname,
61
+ converter: ->(source, result){ result.merge({lastname: source.fullname.split().last }) },
62
+ reverse_converter: ->(source, result){ result }
63
+
51
64
  rule to: :id
65
+
66
+ # or
67
+ # rule to: [:id, :foo, :bar, ...], from: [:rid, :rfoo], converter: [->(){}]
68
+
52
69
  end
53
70
  end
54
71
 
55
- old_user = OldUser.new({id: 1, fullname: "myname" })
72
+ old_user = OldUser.new({id: 1, fullname: "myfirst mysecond", r_role: "admin" })
56
73
  new_user = mapper.load(old_user)
57
74
 
58
-
59
75
  puts old_user
60
76
  puts new_user
61
77
 
62
- new_user.name = "no name"
78
+ new_user.firstname = "no"
79
+ new_user.lastname = "name"
63
80
 
64
81
  puts mapper.dump(new_user)
65
82
 
66
- #<struct OldUser id=1, fullname="myname">
67
- #<struct User id=1, name="myname">
68
- #<struct OldUser id=1, fullname="no name">
83
+ #<struct OldUser id=1, fullname="myfirst mysecond", r_role="admin">
84
+ #<struct User id=1, firstname="myfirst", lastname="mysecond", role="admin">
85
+ #<struct OldUser id=1, fullname="no name", r_role="admin">
86
+
87
+ # the resulting class has no hash enabled constructor?
88
+
89
+ puts "\n" * 5
90
+
91
+ OldUser2 = Struct.new(:id, :fullname, :r_role)
92
+
93
+ User2 = Struct.new(:id, :firstname, :lastname, :role)
94
+
95
+ reverse_source = ->(attrs){
96
+ OldUser2.new.tap do |old_user|
97
+ attrs.each_pair do |key, value|
98
+ old_user.public_send "#{key}=", value
99
+ end
100
+ end
101
+ }
102
+
103
+ source = ->(attrs){
104
+ User2.new.tap do |old_user|
105
+ attrs.each_pair do |key, value|
106
+ old_user.public_send "#{key}=", value
107
+ end
108
+ end
109
+ }
110
+
111
+ mapper = Yaoc::ObjectMapper.new(source, reverse_source).tap do |mapper|
112
+ mapper.add_mapping do
113
+ fetcher :public_send
114
+ rule to: :role, from: :r_role
115
+
116
+ rule to: :firstname,
117
+ from: :fullname,
118
+ converter: ->(source, result){ result.merge({firstname: source.fullname.split().first }) },
119
+ reverse_converter: ->(source, result){ result.merge({fullname: "#{source.firstname} #{source.lastname}" }) }
120
+
121
+ rule to: :lastname,
122
+ from: :fullname,
123
+ converter: ->(source, result){ result.merge({lastname: source.fullname.split().last }) },
124
+ reverse_converter: ->(source, result){ result }
125
+
126
+ rule to: :id
127
+ end
128
+ end
129
+
130
+ old_user2 = OldUser2.new(1, "myfirst mysecond", "admin" )
131
+ new_user2 = mapper.load(old_user)
132
+
133
+ puts old_user2
134
+ puts new_user2
135
+
136
+ new_user2.firstname = "no"
137
+ new_user2.lastname = "name"
138
+
139
+ puts mapper.dump(new_user2)
140
+
141
+ #<struct OldUser2 id=1, fullname="myfirst mysecond", r_role="admin">
142
+ #<struct User2 id=1, firstname="myfirst", lastname="mysecond", role="admin">
143
+ #<struct OldUser2 id=1, fullname="no name", r_role="admin">
69
144
 
70
145
  ```
71
146
 
@@ -4,6 +4,7 @@ require 'yaoc/mapping_base'
4
4
 
5
5
  Dir[File.join(File.expand_path(__dir__ ), "yaoc/strategies/*.rb")].each { |f| require f }
6
6
 
7
+ require 'yaoc/converter_builder'
7
8
  require 'yaoc/object_mapper'
8
9
 
9
10
  module Yaoc
@@ -0,0 +1,73 @@
1
+ module Yaoc
2
+
3
+ class ConverterBuilder
4
+ attr_accessor :build_commands, :command_order
5
+
6
+ def initialize(command_order=:recorded_order, fetcher=:fetch)
7
+ self.build_commands = []
8
+ self.command_order = command_order
9
+ self.fetcher = fetcher
10
+ end
11
+
12
+ def add_mapping(&block)
13
+ instance_eval &block
14
+ apply_commands!
15
+ end
16
+
17
+ def converter_class
18
+ @converter_class ||= Struct.new(:to_convert, :fetcher) do
19
+ include MappingBase
20
+ include Strategies::ToHashMapping
21
+ end
22
+ end
23
+
24
+
25
+ def rule(to: nil, from: to, converter: nil)
26
+ to_s = Array(to)
27
+ from_s = Array(from)
28
+ converter_s = Array(converter)
29
+
30
+ to_s.each_with_index do |to, index|
31
+ build_commands.push ->{ converter_class.map(to, from_s[index] || to, converter_s[index]) }
32
+ end
33
+ end
34
+
35
+ def apply_commands!
36
+ reset_converters!
37
+
38
+ build_commands_ordered.each &:call
39
+ end
40
+
41
+ def converter(fetch_able)
42
+ converter_class.new(fetch_able, fetcher)
43
+ end
44
+
45
+ def fetcher=(new_fetcher)
46
+ @fetcher= new_fetcher
47
+ end
48
+
49
+ protected
50
+
51
+ def fetcher
52
+ @fetcher ||= :fetch
53
+ end
54
+
55
+ def fetch_with(new_fetcher)
56
+ self.fetcher = new_fetcher
57
+ end
58
+
59
+ def build_commands_ordered
60
+ if command_order == :recorded_order
61
+ build_commands
62
+ else
63
+ build_commands.reverse
64
+ end
65
+ end
66
+
67
+ def reset_converters!
68
+ @converter_class = nil
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -1,15 +1,11 @@
1
1
  module Yaoc
2
2
 
3
3
  class ObjectMapper
4
- attr_accessor :load_result_source, :dump_result_source,
5
- :forward_commands, :backward_commads
4
+ attr_accessor :load_result_source, :dump_result_source
6
5
 
7
6
  def initialize(load_result_source, dump_result_source=->(attrs){ attrs})
8
7
  self.load_result_source = load_result_source.respond_to?(:call) ? load_result_source : ->(attrs){load_result_source.new(attrs)}
9
8
  self.dump_result_source = dump_result_source.respond_to?(:call) ? dump_result_source : ->(attrs){dump_result_source.new(attrs)}
10
-
11
- self.forward_commands = []
12
- self.backward_commads = []
13
9
  end
14
10
 
15
11
  def load(fetch_able)
@@ -28,59 +24,46 @@ module Yaoc
28
24
  protected
29
25
 
30
26
  def apply_commands
31
- reset_converters!
32
-
33
- forward_commands.each &:call
34
- backward_commads.each &:call
35
- end
36
-
37
- def reset_converters!
38
- @reverse_converter_class = nil
39
- @converter_class = nil
27
+ converter_builder.apply_commands!
28
+ reverse_converter_builder.apply_commands!
40
29
  end
41
30
 
42
31
  def rule(to: nil, from: to, converter: nil, reverse_converter: nil)
43
- forward_commands.push ->{ converter_class.map(to, from, converter) }
44
- backward_commads.unshift ->{ reverse_converter_class.map(from, to, reverse_converter) }
45
- end
32
+ converter_builder.rule(
33
+ to: to,
34
+ from: from,
35
+ converter: converter,
36
+ )
46
37
 
47
- def fetcher(new_fetcher)
48
- @fetcher= new_fetcher
38
+ reverse_converter_builder.rule(
39
+ to: from,
40
+ from: to,
41
+ converter: reverse_converter,
42
+ )
49
43
  end
50
44
 
51
- def fetcher_method
52
- @fetcher ||= :fetch
45
+ def fetcher(new_fetcher)
46
+ converter_builder.fetcher = new_fetcher
53
47
  end
54
48
 
55
49
  def reverse_fetcher(new_fetcher)
56
- @reverse_fetcher = new_fetcher
57
- end
58
-
59
- def reverse_fetcher_method
60
- @reverse_fetcher ||= :public_send
50
+ reverse_converter_builder.fetcher = new_fetcher
61
51
  end
62
52
 
63
53
  def converter(fetch_able)
64
- converter_class.new(fetch_able, fetcher_method)
65
- end
66
-
67
- def converter_class
68
- @converter_class ||= new_converter_class
54
+ converter_builder.converter(fetch_able)
69
55
  end
70
56
 
71
57
  def reverse_converter(fetch_able)
72
- reverse_converter_class.new(fetch_able, reverse_fetcher_method)
58
+ reverse_converter_builder.converter(fetch_able)
73
59
  end
74
60
 
75
- def reverse_converter_class
76
- @reverse_converter_class ||= new_converter_class
61
+ def converter_builder
62
+ @converter_builder ||= Yaoc::ConverterBuilder.new()
77
63
  end
78
64
 
79
- def new_converter_class
80
- Struct.new(:to_convert, :fetcher) do
81
- include MappingBase
82
- include Strategies::ToHashMapping
83
- end
65
+ def reverse_converter_builder
66
+ @reverse_converter_builder ||= Yaoc::ConverterBuilder.new(:reverse_order, :public_send)
84
67
  end
85
68
 
86
69
  end
@@ -1,3 +1,3 @@
1
1
  module Yaoc
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -16,13 +16,14 @@ feature "Map objects", %q{
16
16
  reverse_converter: ->(source, result){ result.merge({name: source.name}) }
17
17
  rule to: :role, from: :fullrolename
18
18
  rule to: :id
19
+ rule to: [:foo, :bar]
19
20
  end
20
21
  end
21
22
  }
22
23
 
23
24
  given(:load_result_object_class) {
24
- Struct.new(:id, :name, :role) do
25
- include Equalizer.new(:id, :name, :role)
25
+ Struct.new(:id, :name, :role, :foo, :bar) do
26
+ include Equalizer.new(:id, :name, :role, :foo, :bar)
26
27
 
27
28
  def initialize(params={})
28
29
  super()
@@ -36,8 +37,8 @@ feature "Map objects", %q{
36
37
  }
37
38
 
38
39
  given(:dump_result_object_class) {
39
- Struct.new(:id, :name, :fullrolename) do
40
- include Equalizer.new(:id, :name, :fullrolename)
40
+ Struct.new(:id, :name, :fullrolename, :foo, :bar) do
41
+ include Equalizer.new(:id, :name, :fullrolename, :foo, :bar)
41
42
 
42
43
  def initialize(params={})
43
44
  super()
@@ -55,11 +56,11 @@ feature "Map objects", %q{
55
56
  }
56
57
 
57
58
  given(:load_result_object){
58
- load_result_object_class.new({id: 1, name: "paul", role: "admin"})
59
+ load_result_object_class.new({id: 1, name: "paul", role: "admin", foo: "some thing", bar: "some other thing"})
59
60
  }
60
61
 
61
62
  given(:dump_result_object){
62
- dump_result_object_class.new({id: 1, name: "paul", fullrolename: "admin"})
63
+ dump_result_object_class.new({id: 1, name: "paul", fullrolename: "admin", foo: "some thing", bar: "some other thing"})
63
64
  }
64
65
 
65
66
  scenario "creates an result object from an input_object" do
@@ -0,0 +1,108 @@
1
+ require "spec_helper"
2
+
3
+ describe Yaoc::ConverterBuilder do
4
+ subject{
5
+ Yaoc::ConverterBuilder.new().tap{|converter|
6
+ converter.stub(:converter_class).and_return(converter_class)
7
+ }
8
+ }
9
+
10
+
11
+ let(:converter_class){
12
+ double("converter_class", map: nil, new: converter)
13
+ }
14
+
15
+ let(:converter){
16
+ double("converter", call: nil)
17
+ }
18
+
19
+ describe ".new" do
20
+ it "applies command in recorded order as default" do
21
+
22
+ expect(converter_class).to receive(:map).ordered.with(:id, :id, nil)
23
+ expect(converter_class).to receive(:map).ordered.with(:name, :name, nil)
24
+
25
+ subject.add_mapping do
26
+ rule to: :id
27
+ rule to: :name
28
+ end
29
+
30
+ end
31
+
32
+ it "applies command in reverse recorded order when wanted" do
33
+ subject = Yaoc::ConverterBuilder.new(:reverse_order).tap{|converter|
34
+ converter.stub(:converter_class).and_return(converter_class)
35
+ }
36
+
37
+
38
+ expect(converter_class).to receive(:map).ordered.with(:name, :name, nil)
39
+ expect(converter_class).to receive(:map).ordered.with(:id, :id, nil)
40
+
41
+ subject.add_mapping do
42
+ rule to: :id
43
+ rule to: :name
44
+ end
45
+
46
+ end
47
+ end
48
+
49
+ describe "#add_mapping" do
50
+
51
+
52
+
53
+ it "delegates to internal methods" do
54
+ expect(subject).to receive(:fetch_with).with(:public_send)
55
+ expect(subject).to receive(:rule).with(to: :id, from: :from, converter: :converter)
56
+
57
+ subject.add_mapping do
58
+ fetch_with :public_send
59
+ rule to: :id, from: :from, converter: :converter
60
+ end
61
+ end
62
+
63
+
64
+
65
+ end
66
+
67
+ describe "#rule" do
68
+ it "creates a converter" do
69
+
70
+ expect(converter_class).to receive(:map).with(:id, :id2, :some_proc)
71
+
72
+ subject.add_mapping do
73
+ rule to: :id,
74
+ from: :id2,
75
+ converter: :some_proc
76
+ end
77
+
78
+ end
79
+
80
+ it "uses defaults" do
81
+ expect(converter_class).to receive(:map).with(:id, :id, nil)
82
+
83
+ subject.add_mapping do
84
+ rule to: :id
85
+ end
86
+
87
+ end
88
+
89
+ it "allows to use array of attributes" do
90
+ expect(converter_class).to receive(:map).ordered.with(:id, :id, nil)
91
+ expect(converter_class).to receive(:map).ordered.with(:name, :name, nil)
92
+
93
+ subject.add_mapping do
94
+ rule to: [:id, :name]
95
+ end
96
+ end
97
+
98
+ it "use the right to when from in arrays is missing" do
99
+ expect(converter_class).to receive(:map).ordered.with(:id, :r_id, nil)
100
+ expect(converter_class).to receive(:map).ordered.with(:name, :name, nil)
101
+
102
+ subject.add_mapping do
103
+ rule to: [:id, :name],
104
+ from: [:r_id]
105
+ end
106
+ end
107
+ end
108
+ end
@@ -3,17 +3,17 @@ require "spec_helper"
3
3
  describe Yaoc::ObjectMapper do
4
4
  subject{
5
5
  Yaoc::ObjectMapper.new(Struct.new(:id, :name)).tap{|mapper|
6
- mapper.stub(:converter_class).and_return(converter_class)
7
- mapper.stub(:reverse_converter_class).and_return(reverse_converter_class)
6
+ mapper.stub(:converter_builder).and_return(converter_builder)
7
+ mapper.stub(:reverse_converter_builder).and_return(reverse_converter_builder)
8
8
  }
9
9
  }
10
10
 
11
- let(:converter_class){
12
- double("converter_class", map: nil, new: converter)
11
+ let(:converter_builder){
12
+ double("converter_builder", rule: nil, apply_commands!: nil, converter: converter)
13
13
  }
14
14
 
15
- let(:reverse_converter_class){
16
- double("reverse_converter_class", map: nil, new: reverse_converter)
15
+ let(:reverse_converter_builder){
16
+ double("reverse_converter_builder", rule: nil, apply_commands!: nil, converter: reverse_converter)
17
17
  }
18
18
 
19
19
  let(:converter){
@@ -26,9 +26,10 @@ describe Yaoc::ObjectMapper do
26
26
 
27
27
  describe "#add_mapping" do
28
28
 
29
- it "creates a mapper" do
29
+ it "creates a converter" do
30
+
31
+ expect(converter_builder).to receive(:rule).with(to: :id, from: :id2, converter: :some_proc)
30
32
 
31
- expect(converter_class).to receive(:map).with(:id, :id2, :some_proc)
32
33
  subject.add_mapping do
33
34
  rule to: :id,
34
35
  from: :id2,
@@ -38,9 +39,9 @@ describe Yaoc::ObjectMapper do
38
39
 
39
40
  end
40
41
 
41
- it "creates a revers mapper" do
42
+ it "creates a revers converter" do
42
43
 
43
- expect(reverse_converter_class).to receive(:map).with(:id2, :id, :some_reverse_proc)
44
+ expect(reverse_converter_builder).to receive(:rule).with(to: :id2, from: :id, converter: :some_reverse_proc)
44
45
 
45
46
  subject.add_mapping do
46
47
  rule to: :id,
@@ -52,8 +53,8 @@ describe Yaoc::ObjectMapper do
52
53
  end
53
54
 
54
55
  it "uses defaults" do
55
- expect(converter_class).to receive(:map).with(:id, :id, nil)
56
- expect(reverse_converter_class).to receive(:map).with(:id, :id, nil)
56
+ expect(converter_builder).to receive(:rule).with(to: :id, from: :id, converter: nil)
57
+ expect(reverse_converter_builder).to receive(:rule).with(to: :id, from: :id, converter: nil)
57
58
 
58
59
  subject.add_mapping do
59
60
  rule to: :id
@@ -62,21 +63,23 @@ describe Yaoc::ObjectMapper do
62
63
  end
63
64
 
64
65
  it "allows to set a fetcher" do
66
+ expect(converter_builder).to receive(:fetcher=).with(:public_send)
67
+
65
68
  subject.add_mapping do
66
69
  fetcher :public_send
67
70
  rule to: :id
68
71
  end
69
72
 
70
- expect(subject.send :fetcher_method).to eq(:public_send)
73
+
71
74
  end
72
75
 
73
76
  it "allows to set a reverse_fetcher" do
77
+ expect(reverse_converter_builder).to receive(:fetcher=).with(:fetch)
78
+
74
79
  subject.add_mapping do
75
80
  reverse_fetcher :fetch
76
81
  rule to: :id
77
82
  end
78
-
79
- expect(subject.send :reverse_fetcher_method).to eq(:fetch)
80
83
  end
81
84
  end
82
85
 
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.2
4
+ version: 0.0.3
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-01-22 00:00:00.000000000 Z
11
+ date: 2014-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: abstract_type
@@ -220,6 +220,7 @@ files:
220
220
  - README.md
221
221
  - Rakefile
222
222
  - lib/yaoc.rb
223
+ - lib/yaoc/converter_builder.rb
223
224
  - lib/yaoc/mapping_base.rb
224
225
  - lib/yaoc/object_mapper.rb
225
226
  - lib/yaoc/strategies/to_array_mapping.rb
@@ -228,6 +229,7 @@ files:
228
229
  - spec/acceptance/map_objects_spec.rb
229
230
  - spec/spec_helper.rb
230
231
  - spec/support/feature.rb
232
+ - spec/unit/lib/yaoc/converter_builder_spec.rb
231
233
  - spec/unit/lib/yaoc/mapping_base_spec.rb
232
234
  - spec/unit/lib/yaoc/object_mapper_spec.rb
233
235
  - spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb
@@ -261,6 +263,7 @@ test_files:
261
263
  - spec/acceptance/map_objects_spec.rb
262
264
  - spec/spec_helper.rb
263
265
  - spec/support/feature.rb
266
+ - spec/unit/lib/yaoc/converter_builder_spec.rb
264
267
  - spec/unit/lib/yaoc/mapping_base_spec.rb
265
268
  - spec/unit/lib/yaoc/object_mapper_spec.rb
266
269
  - spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb