yaoc 0.0.5 → 0.0.6

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: 1a3e08de6de1dd93ced77b872cc67f802987d027
4
- data.tar.gz: 01fa46d585b98a34f3c13fb118d259b6006a8d58
3
+ metadata.gz: 12e7c498b11d4f4f205c85146f98f715d49d9f52
4
+ data.tar.gz: aba743b04f8bb624c80758aa6f4466ad467ae4d9
5
5
  SHA512:
6
- metadata.gz: 045204c3589d7c9c683ba014503db36c6dab8165a34abd50a1e7157f1b28445136a7923a62d2b6b2506bdd6276fc2989fc0cfcdb7e6b725c45cafc4e9edcb05d
7
- data.tar.gz: 31cd8c25c0f8a7d6dc1ecd960777fd34e1b9d2f3b9cb3b1663673724ae59d6aa4e446efe9730cf625c23e66e9b4af573d71db25cf3c78412efce0aac67577fb8
6
+ metadata.gz: 05c3efaa5c1e2862b10194039dbaca983dfdd366fab365aab1a1aa93551265aa286f68387979d69f939409dec3c21bb28f79a876a851855979116afb8d4dfd22
7
+ data.tar.gz: b4919d5988c9b6cc7c9e36a082e5fe8b2c9eaf712157533a85736c7bf9a081a4a90b90caaa6239b56d62612f6654af67dc67dfaa5c3d6ea3e6b7924492ef03ef
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ notifications:
5
+ recipients:
6
+ - shad0wrunner@gmx.de
data/Gemfile CHANGED
@@ -5,3 +5,4 @@ ruby "2.1.0"
5
5
  gemspec
6
6
 
7
7
  gem 'simplecov', require: false, group: :test
8
+ gem 'coveralls', require: false
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Yaoc
1
+ # Yaoc [![Code Climate](https://codeclimate.com/github/slowjack2k/yaoc.png)](https://codeclimate.com/github/slowjack2k/yaoc) [![Build Status](https://travis-ci.org/slowjack2k/yaoc.png?branch=master)](https://travis-ci.org/slowjack2k/yaoc) [![Coverage Status](https://coveralls.io/repos/slowjack2k/yaoc/badge.png?branch=master)](https://coveralls.io/r/slowjack2k/yaoc?branch=master) [![Gem Version](https://badge.fury.io/rb/yaoc.png)](http://badge.fury.io/rb/yaoc)
2
2
 
3
3
  Indentation of this gem is to learn and train a little ruby.
4
4
 
@@ -26,25 +26,11 @@ Uptodate doc's look into the specs.
26
26
 
27
27
  require 'yaoc'
28
28
 
29
- User = Struct.new(:id, :firstname, :lastname, :role) do
30
- def initialize(params={})
31
- super()
29
+ include Yaoc::Helper
32
30
 
33
- params.each do |attr, value|
34
- self.public_send("#{attr}=", value)
35
- end if params
36
- end
37
- end
38
-
39
- OldUser = Struct.new(:id, :fullname, :r_role) do
40
- def initialize(params={})
41
- super()
31
+ User = StructHE(:id, :firstname, :lastname, :role)
42
32
 
43
- params.each do |attr, value|
44
- self.public_send("#{attr}=", value)
45
- end if params
46
- end
47
- end
33
+ OldUser = StructHE(:id, :fullname, :r_role)
48
34
 
49
35
  mapper = Yaoc::ObjectMapper.new(User, OldUser).tap do |mapper|
50
36
  mapper.add_mapping do
@@ -87,6 +73,10 @@ puts mapper.dump(new_user)
87
73
 
88
74
  ```ruby
89
75
 
76
+ require 'yaoc'
77
+
78
+ include Yaoc::Helper
79
+
90
80
  OldUser2 = Struct.new(:id, :fullname, :r_role)
91
81
 
92
82
  User2 = Struct.new(:id, :firstname, :lastname, :role)
@@ -127,7 +117,7 @@ mapper = Yaoc::ObjectMapper.new(source, reverse_source).tap do |mapper|
127
117
  end
128
118
 
129
119
  old_user2 = OldUser2.new(1, "myfirst mysecond", "admin" )
130
- new_user2 = mapper.load(old_user)
120
+ new_user2 = mapper.load(old_user2)
131
121
 
132
122
  puts old_user2
133
123
  puts new_user2
@@ -137,6 +127,7 @@ new_user2.lastname = "name"
137
127
 
138
128
  puts mapper.dump(new_user2)
139
129
 
130
+
140
131
  #<struct OldUser2 id=1, fullname="myfirst mysecond", r_role="admin">
141
132
  #<struct User2 id=1, firstname="myfirst", lastname="mysecond", role="admin">
142
133
  #<struct OldUser2 id=1, fullname="no name", r_role="admin">
@@ -146,6 +137,13 @@ puts mapper.dump(new_user2)
146
137
  ### But my classes have positional constructor, what now?
147
138
 
148
139
  ```ruby
140
+
141
+ require 'yaoc'
142
+
143
+ include Yaoc::Helper
144
+
145
+ puts "\n" * 5
146
+
149
147
  OldUser3 = Struct.new(:id, :fullname, :r_role)
150
148
  User3 = Struct.new(:id, :firstname, :lastname, :role)
151
149
 
@@ -179,7 +177,7 @@ mapper = Yaoc::ObjectMapper.new(User3, OldUser3).tap do |mapper|
179
177
  end
180
178
 
181
179
  old_user3 = OldUser3.new(1, "myfirst mysecond", "admin" )
182
- new_user3 = mapper.load(old_user)
180
+ new_user3 = mapper.load(old_user3)
183
181
 
184
182
  puts old_user3
185
183
  puts new_user3
@@ -199,46 +197,22 @@ puts mapper.dump(new_user3)
199
197
 
200
198
  ```ruby
201
199
 
202
- User4 = Struct.new(:id, :firstname, :lastname, :roles) do
203
- def initialize(params={})
204
- super()
200
+ require 'yaoc'
205
201
 
206
- params.each do |attr, value|
207
- self.public_send("#{attr}=", value)
208
- end if params
209
- end
210
- end
202
+ include Yaoc::Helper
211
203
 
212
- OldUser4 = Struct.new(:o_id, :o_firstname, :o_lastname, :o_roles) do
213
- def initialize(params={})
214
- super()
215
204
 
216
- params.each do |attr, value|
217
- self.public_send("#{attr}=", value)
218
- end if params
219
- end
220
- end
205
+ puts "\n" * 5
221
206
 
222
207
 
223
- Role = Struct.new(:id, :name) do
224
- def initialize(params={})
225
- super()
208
+ User4 = StructHE(:id, :firstname, :lastname, :roles)
226
209
 
227
- params.each do |attr, value|
228
- self.public_send("#{attr}=", value)
229
- end if params
230
- end
231
- end
210
+ OldUser4 = StructHE(:o_id, :o_firstname, :o_lastname, :o_roles)
232
211
 
233
- OldRole = Struct.new(:o_id, :o_name) do
234
- def initialize(params={})
235
- super()
236
212
 
237
- params.each do |attr, value|
238
- self.public_send("#{attr}=", value)
239
- end if params
240
- end
241
- end
213
+ Role = StructHE(:id, :name)
214
+
215
+ OldRole = StructHE(:o_id, :o_name)
242
216
 
243
217
 
244
218
  role_mapper = Yaoc::ObjectMapper.new(Role, OldRole).tap do |mapper|
@@ -287,6 +261,52 @@ puts user_mapper.dump(new_user4)
287
261
 
288
262
  ```
289
263
 
264
+ ### And how can I add values to existing objects?
265
+
266
+ ```ruby
267
+ require 'yaoc'
268
+
269
+ include Yaoc::Helper
270
+
271
+ puts "\n" * 5
272
+
273
+ OldUser5 = StructHE(:id, :name)
274
+
275
+ RoleThing = StructHE(:id, :role)
276
+
277
+ User5 = StructHE(:id, :name, :role)
278
+
279
+
280
+ user_mapper = Yaoc::ObjectMapper.new(User5, OldUser5).tap do |mapper|
281
+ mapper.add_mapping do
282
+ fetcher :public_send
283
+ rule to: [:id, :name]
284
+ end
285
+ end
286
+
287
+ role_mapper = Yaoc::ObjectMapper.new(User5, RoleThing).tap do |mapper|
288
+ mapper.add_mapping do
289
+ fetcher :public_send
290
+ rule to: [:role]
291
+ end
292
+ end
293
+
294
+ old_role = RoleThing.new(id: 1, role: "my_role")
295
+ old_user5 = OldUser5.new(id: 1, name: "my fullname")
296
+ new_user5 = user_mapper.load(old_user5)
297
+
298
+ role_mapper.load(old_role, new_user5)
299
+
300
+ puts old_user5
301
+ puts old_role
302
+ puts new_user5
303
+
304
+ #<struct OldUser5 id=1, name="my fullname">
305
+ #<struct RoleThing id=1, role="my_role">
306
+ #<struct User5 id=1, name="my fullname", role="my_role">
307
+
308
+ ```
309
+
290
310
  ## Contributing
291
311
 
292
312
  1. Fork it ( http://github.com/slowjack2k/yaoc/fork )
@@ -0,0 +1,44 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:development)
3
+
4
+ require 'yaoc'
5
+
6
+ include Yaoc::Helper
7
+
8
+ User = StructHE(:id, :firstname, :lastname, :role)
9
+
10
+ OldUser = StructHE(:id, :fullname, :r_role)
11
+
12
+ mapper = Yaoc::ObjectMapper.new(User, OldUser).tap do |mapper|
13
+ mapper.add_mapping do
14
+ fetcher :public_send
15
+ rule to: :role, from: :r_role
16
+
17
+ rule to: :firstname,
18
+ from: :fullname,
19
+ converter: ->(source, result){ fill_result_with_value(result, :firstname, source.fullname.split().first) },
20
+ reverse_converter: ->(source, result){ fill_result_with_value(result, :fullname, "#{source.firstname} #{source.lastname}") }
21
+
22
+ rule to: :lastname,
23
+ from: :fullname,
24
+ converter: ->(source, result){ fill_result_with_value(result, :lastname, source.fullname.split().last ) },
25
+ reverse_converter: ->(source, result){ result }
26
+
27
+ rule to: :id
28
+ end
29
+ end
30
+
31
+ old_user = OldUser.new({id: 1, fullname: "myfirst mysecond", r_role: "admin" })
32
+ new_user = mapper.load(old_user)
33
+
34
+ puts "\n" * 5
35
+
36
+ puts old_user
37
+ puts new_user
38
+
39
+ new_user.firstname = "no"
40
+ new_user.lastname = "name"
41
+
42
+ puts mapper.dump(new_user)
43
+
44
+ puts "\n" * 5
@@ -0,0 +1,62 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:development)
3
+
4
+ require 'yaoc'
5
+
6
+ include Yaoc::Helper
7
+
8
+ puts "\n" * 5
9
+
10
+ OldUser2 = Struct.new(:id, :fullname, :r_role)
11
+
12
+ User2 = Struct.new(:id, :firstname, :lastname, :role)
13
+
14
+ reverse_source = ->(attrs){
15
+ OldUser2.new.tap do |old_user|
16
+ attrs.each_pair do |key, value|
17
+ old_user.public_send "#{key}=", value
18
+ end
19
+ end
20
+ }
21
+
22
+ source = ->(attrs){
23
+ User2.new.tap do |old_user|
24
+ attrs.each_pair do |key, value|
25
+ old_user.public_send "#{key}=", value
26
+ end
27
+ end
28
+ }
29
+
30
+ mapper = Yaoc::ObjectMapper.new(source, reverse_source).tap do |mapper|
31
+ mapper.add_mapping do
32
+ fetcher :public_send
33
+ rule to: :role, from: :r_role
34
+
35
+ rule to: :firstname,
36
+ from: :fullname,
37
+ converter: ->(source, result){ fill_result_with_value(result, :firstname, source.fullname.split().first ) },
38
+ reverse_converter: ->(source, result){ fill_result_with_value(result, :fullname, "#{source.firstname} #{source.lastname}") }
39
+
40
+ rule to: :lastname,
41
+ from: :fullname,
42
+ converter: ->(source, result){ fill_result_with_value(result, :lastname, source.fullname.split().last) },
43
+ reverse_converter: ->(source, result){ result }
44
+
45
+ rule to: :id
46
+ end
47
+ end
48
+
49
+ old_user2 = OldUser2.new(1, "myfirst mysecond", "admin" )
50
+ new_user2 = mapper.load(old_user2)
51
+
52
+ puts old_user2
53
+ puts new_user2
54
+
55
+ new_user2.firstname = "no"
56
+ new_user2.lastname = "name"
57
+
58
+ puts mapper.dump(new_user2)
59
+
60
+
61
+
62
+ puts "\n" * 5
@@ -0,0 +1,58 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:development)
3
+
4
+ require 'yaoc'
5
+
6
+ include Yaoc::Helper
7
+
8
+ puts "\n" * 5
9
+
10
+ OldUser3 = Struct.new(:id, :fullname, :r_role)
11
+ User3 = Struct.new(:id, :firstname, :lastname, :role)
12
+
13
+
14
+ mapper = Yaoc::ObjectMapper.new(User3, OldUser3).tap do |mapper|
15
+ mapper.add_mapping do
16
+ fetcher :public_send
17
+
18
+ strategy :to_array_mapping
19
+ reverse_strategy :to_array_mapping
20
+
21
+ rule to: 0, from: :id,
22
+ reverse_to: 0, reverse_from: :id
23
+
24
+ rule to: 1,
25
+ from: :fullname,
26
+
27
+ converter: ->(source, result){ fill_result_with_value(result, 1, source.fullname.split().first) },
28
+ reverse_converter: ->(source, result){ fill_result_with_value(result, 1, "#{source.firstname} #{source.lastname}") }
29
+
30
+ rule to: 2,
31
+ from: :fullname,
32
+
33
+ converter: ->(source, result){ result[2] = source.fullname.split().last },
34
+ reverse_converter: ->(source, result){ result }
35
+
36
+ rule to: 3, from: :r_role,
37
+ reverse_to: 2, reverse_from: :role
38
+
39
+ end
40
+ end
41
+
42
+ old_user3 = OldUser3.new(1, "myfirst mysecond", "admin" )
43
+ new_user3 = mapper.load(old_user3)
44
+
45
+ puts old_user3
46
+ puts new_user3
47
+
48
+ new_user3.firstname = "no"
49
+ new_user3.lastname = "name"
50
+
51
+ puts mapper.dump(new_user3)
52
+
53
+
54
+ puts "\n" * 5
55
+
56
+
57
+
58
+
@@ -0,0 +1,59 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:development)
3
+
4
+ require 'yaoc'
5
+
6
+ include Yaoc::Helper
7
+
8
+
9
+ puts "\n" * 5
10
+
11
+
12
+ User4 = StructHE(:id, :firstname, :lastname, :roles)
13
+
14
+ OldUser4 = StructHE(:o_id, :o_firstname, :o_lastname, :o_roles)
15
+
16
+
17
+ Role = StructHE(:id, :name)
18
+
19
+ OldRole = StructHE(:o_id, :o_name)
20
+
21
+
22
+ role_mapper = Yaoc::ObjectMapper.new(Role, OldRole).tap do |mapper|
23
+ mapper.add_mapping do
24
+ fetcher :public_send
25
+
26
+ rule to: :id, from: :o_id
27
+ rule to: :name, from: :o_name
28
+
29
+ end
30
+ end
31
+
32
+ user_mapper = Yaoc::ObjectMapper.new(User4, OldUser4).tap do |mapper|
33
+ mapper.add_mapping do
34
+ fetcher :public_send
35
+
36
+ rule to: [:id, :firstname, :lastname],
37
+ from: [:o_id, :o_firstname, :o_lastname]
38
+
39
+ rule to: :roles,
40
+ from: :o_roles,
41
+ object_converter: role_mapper,
42
+ is_collection: true
43
+
44
+ end
45
+ end
46
+
47
+
48
+ old_user4 = OldUser4.new(o_id: 1,
49
+ o_firstname: "firstname",
50
+ o_lastname:"lastname",
51
+ o_roles: [OldRole.new(o_id: 1, o_name: "admin"), OldRole.new(o_id: 2, o_name: "guest")] )
52
+ new_user4 = user_mapper.load(old_user4)
53
+
54
+ puts old_user4
55
+ puts new_user4
56
+
57
+ puts user_mapper.dump(new_user4)
58
+
59
+ puts "\n" * 5
@@ -0,0 +1,47 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:development)
3
+
4
+ require 'yaoc'
5
+
6
+ include Yaoc::Helper
7
+
8
+ puts "\n" * 5
9
+
10
+ OldUser5 = StructHE(:id, :name)
11
+
12
+ RoleThing = StructHE(:id, :role)
13
+
14
+ User5 = StructHE(:id, :name, :role)
15
+
16
+
17
+ user_mapper = Yaoc::ObjectMapper.new(User5, OldUser5).tap do |mapper|
18
+ mapper.add_mapping do
19
+ fetcher :public_send
20
+ rule to: [:id, :name]
21
+ end
22
+ end
23
+
24
+ role_mapper = Yaoc::ObjectMapper.new(User5, RoleThing).tap do |mapper|
25
+ mapper.add_mapping do
26
+ fetcher :public_send
27
+ rule to: [:role]
28
+ end
29
+ end
30
+
31
+ old_role = RoleThing.new(id: 1, role: "my_role")
32
+ old_user5 = OldUser5.new(id: 1, name: "my fullname")
33
+ new_user5 = user_mapper.load(old_user5)
34
+
35
+ role_mapper.load(old_role, new_user5)
36
+
37
+ puts old_user5
38
+ puts old_role
39
+ puts new_user5
40
+
41
+
42
+
43
+ puts "\n" * 5
44
+
45
+
46
+
47
+
@@ -8,6 +8,8 @@ Dir[File.join(File.expand_path(__dir__ ), "yaoc/strategies/*.rb")].each { |f| re
8
8
  require 'yaoc/converter_builder'
9
9
  require 'yaoc/object_mapper'
10
10
 
11
+ Dir[File.join(File.expand_path(__dir__ ), "yaoc/helper/*.rb")].each { |f| require f }
12
+
11
13
  module Yaoc
12
14
 
13
15
  end
@@ -0,0 +1,33 @@
1
+ module Yaoc
2
+ module Helper
3
+ module StructHashConstructor
4
+ def self.included(klass)
5
+ klass.send :prepend, Initializer
6
+ end
7
+
8
+ module Initializer
9
+ def initialize(params={})
10
+ super()
11
+
12
+ params.each do |attr, value|
13
+ self.public_send("#{attr}=", value)
14
+ end if params
15
+ end
16
+ end
17
+ end
18
+
19
+ module_function
20
+ def StructH(*args, &block)
21
+ Struct.new(*args, &block).tap do|new_class|
22
+ new_class.send(:include, Yaoc::Helper::StructHashConstructor)
23
+ end
24
+ end
25
+
26
+ def StructHE(*args, &block)
27
+ StructH(*args, &block).tap do|new_class|
28
+ include Equalizer.new(*args)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -7,22 +7,39 @@ module Yaoc
7
7
  end
8
8
 
9
9
  module InstanceMethods
10
- def call(*args)
11
- create_target(super)
10
+ def call(pre_created_object=nil)
11
+ source_converted_to_hash_or_array = super()
12
+ if pre_created_object.nil?
13
+ create_target_from_class(source_converted_to_hash_or_array)
14
+ else
15
+ fill_target_object(source_converted_to_hash_or_array, pre_created_object)
16
+ end
12
17
  end
13
18
 
14
19
  def source_method
15
20
  self.target_source.respond_to?(:call) ? :call : :new
16
21
  end
17
22
 
18
- def create_target(args)
19
- if args.is_a? Array
23
+ def create_target_from_class(args)
24
+ array_based_constructor = args.is_a? Array
25
+
26
+ if array_based_constructor
20
27
  self.target_source.send(source_method, *args)
21
28
  else
22
29
  self.target_source.send(source_method, args)
23
30
  end
24
31
  end
25
32
 
33
+ def fill_target_object(attribute_hash, pre_created_object)
34
+ raise "UnexpectedStrategy" unless attribute_hash.respond_to? :each_pair
35
+
36
+ attribute_hash.each_pair do |key, value|
37
+ pre_created_object.send("#{key}=", value)
38
+ end
39
+
40
+ pre_created_object
41
+ end
42
+
26
43
  def to_a # wenn included into struct's Array(...) call's to_a
27
44
  [self]
28
45
  end
@@ -8,12 +8,12 @@ module Yaoc
8
8
  self.dump_result_source = dump_result_source
9
9
  end
10
10
 
11
- def load(fetch_able)
12
- converter(fetch_able).call()
11
+ def load(fetch_able, object_to_fill=nil)
12
+ converter(fetch_able).call(object_to_fill)
13
13
  end
14
14
 
15
- def dump(object)
16
- reverse_converter(object).call()
15
+ def dump(object, object_to_fill=nil)
16
+ reverse_converter(object).call(object_to_fill)
17
17
  end
18
18
 
19
19
  def add_mapping(&block)
@@ -1,3 +1,3 @@
1
1
  module Yaoc
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -0,0 +1,79 @@
1
+ require "spec_helper"
2
+
3
+ feature "Fill existing objects", %q{
4
+ In order to use this lib with pre created objects
5
+ as a lib user
6
+ I want to be able to fill exiting objects instead of create a new one
7
+ } do
8
+
9
+ given(:new_user_class){
10
+ Yaoc::Helper::StructHE(:id, :firstname, :lastname, :roles)
11
+ }
12
+
13
+ given(:old_user_class){
14
+ Yaoc::Helper::StructHE(:o_id, :o_firstname, :o_lastname)
15
+ }
16
+
17
+ given(:user_converter){
18
+
19
+ Yaoc::ObjectMapper.new(new_user_class, old_user_class).tap do |mapper|
20
+ mapper.add_mapping do
21
+ fetcher :public_send
22
+
23
+ rule to: [:id, :firstname, :lastname],
24
+ from: [:o_id, :o_firstname, :o_lastname]
25
+
26
+ end
27
+ end
28
+ }
29
+
30
+ given(:expected_new_user) {
31
+ new_user_class.new(
32
+ id: "user_1",
33
+ firstname: "o firstname",
34
+ lastname: "o lastname",
35
+ roles: "admin, ruth, guest"
36
+ )
37
+ }
38
+
39
+ given(:existing_user){
40
+ new_user_class.new(
41
+ id: nil,
42
+ firstname: nil,
43
+ lastname: nil,
44
+ roles: "admin, ruth, guest"
45
+ )
46
+ }
47
+
48
+ given(:existing_old_user){
49
+ old_user_class.new(
50
+ o_id: "existing_user_2",
51
+ o_firstname: "o existing_firstname",
52
+ o_lastname: "o existing_lastname"
53
+ )
54
+ }
55
+
56
+ given(:old_user) {
57
+ old_user_class.new(
58
+ o_id: "user_1",
59
+ o_firstname: "o firstname",
60
+ o_lastname: "o lastname"
61
+
62
+ )
63
+ }
64
+
65
+ scenario "creates an result object from an input_object" do
66
+ conversion_result = user_converter.load(old_user, existing_user)
67
+
68
+ expect(conversion_result.object_id).to eq existing_user.object_id
69
+ expect(conversion_result).to eq expected_new_user
70
+ end
71
+
72
+ scenario "dumps an result object as result object" do
73
+ conversion_result = user_converter.dump(expected_new_user, existing_old_user)
74
+
75
+ expect(conversion_result.object_id).to eq existing_old_user.object_id
76
+ expect(conversion_result).to eq old_user
77
+ end
78
+
79
+ end
@@ -22,33 +22,11 @@ feature "Map objects", %q{
22
22
  }
23
23
 
24
24
  given(:load_result_object_class) {
25
- Struct.new(:id, :name, :role, :foo, :bar) do
26
- include Equalizer.new(:id, :name, :role, :foo, :bar)
27
-
28
- def initialize(params={})
29
- super()
30
-
31
- params.each do |attr, value|
32
- self.public_send("#{attr}=", value)
33
- end if params
34
- end
35
-
36
- end
25
+ Yaoc::Helper::StructHE(:id, :name, :role, :foo, :bar)
37
26
  }
38
27
 
39
28
  given(:dump_result_object_class) {
40
- Struct.new(:id, :name, :fullrolename, :foo, :bar) do
41
- include Equalizer.new(:id, :name, :fullrolename, :foo, :bar)
42
-
43
- def initialize(params={})
44
- super()
45
-
46
- params.each do |attr, value|
47
- self.public_send("#{attr}=", value)
48
- end if params
49
- end
50
-
51
- end
29
+ Yaoc::Helper::StructHE(:id, :name, :fullrolename, :foo, :bar)
52
30
  }
53
31
 
54
32
  given(:input_object){
@@ -8,31 +8,11 @@ feature "Map objects reusing other existing converters", %q{
8
8
 
9
9
 
10
10
  given(:new_role_class){
11
- Struct.new(:id, :name) do
12
- include Equalizer.new(:id, :name)
13
-
14
- def initialize(params={})
15
- super()
16
-
17
- params.each do |attr, value|
18
- self.public_send("#{attr}=", value)
19
- end if params
20
- end
21
- end
11
+ Yaoc::Helper::StructHE(:id, :name)
22
12
  }
23
13
 
24
14
  given(:old_role_class){
25
- Struct.new(:o_id, :o_name) do
26
- include Equalizer.new(:o_id, :o_name)
27
-
28
- def initialize(params={})
29
- super()
30
-
31
- params.each do |attr, value|
32
- self.public_send("#{attr}=", value)
33
- end if params
34
- end
35
- end
15
+ Yaoc::Helper::StructHE(:o_id, :o_name)
36
16
  }
37
17
 
38
18
  given(:role_converter){
@@ -48,31 +28,11 @@ feature "Map objects reusing other existing converters", %q{
48
28
  }
49
29
 
50
30
  given(:new_user_class){
51
- Struct.new(:id, :firstname, :lastname, :roles) do
52
- include Equalizer.new(:id, :firstname, :lastname, :roles)
53
-
54
- def initialize(params={})
55
- super()
56
-
57
- params.each do |attr, value|
58
- self.public_send("#{attr}=", value)
59
- end if params
60
- end
61
- end
31
+ Yaoc::Helper::StructHE(:id, :firstname, :lastname, :roles)
62
32
  }
63
33
 
64
34
  given(:old_user_class){
65
- Struct.new(:o_id, :o_firstname, :o_lastname, :o_roles) do
66
- include Equalizer.new(:o_id, :o_firstname, :o_lastname, :o_roles)
67
-
68
- def initialize(params={})
69
- super()
70
-
71
- params.each do |attr, value|
72
- self.public_send("#{attr}=", value)
73
- end if params
74
- end
75
- end
35
+ Yaoc::Helper::StructHE(:o_id, :o_firstname, :o_lastname, :o_roles)
76
36
  }
77
37
 
78
38
  given(:user_converter){
@@ -30,17 +30,8 @@ feature "Map objects to classes with positional constructors", %q{
30
30
  }
31
31
 
32
32
  given(:dump_result_object_class) {
33
- Struct.new(:id, :name) do
33
+ Yaoc::Helper::StructH(:id, :name) do
34
34
  include Equalizer.new(:id, :name)
35
-
36
- def initialize(params={})
37
- super()
38
-
39
- params.each do |attr, value|
40
- self.public_send("#{attr}=", value)
41
- end if params
42
- end
43
-
44
35
  end
45
36
  }
46
37
 
@@ -10,6 +10,9 @@ begin
10
10
 
11
11
  add_filter "/spec/"
12
12
  end
13
+ else
14
+ require 'coveralls'
15
+ Coveralls.wear!
13
16
  end
14
17
  rescue LoadError
15
18
  warn "=" * 80
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yaoc::Helper::StructHashConstructor do
4
+ subject{
5
+ Yaoc::Helper::StructH(:id, :name).new(id: 1, name: "no name")
6
+ }
7
+
8
+ it "creates a struct with a hash enabled constructor" do
9
+ expect(subject.id).to eq 1
10
+ expect(subject.name).to eq "no name"
11
+ end
12
+
13
+ context "with equal support" do
14
+ subject{
15
+ Yaoc::Helper::StructHE(:id, :name)
16
+ }
17
+
18
+ it "returns true when all attributes are equal" do
19
+ first = subject.new(id: 1, name: "no name")
20
+ second = subject.new(id: 1, name: "no name")
21
+
22
+ expect(first).to eq second
23
+ end
24
+
25
+ it "returns false when not all atributes are equal" do
26
+ first = subject.new(id: 1, name: "no name")
27
+ second = subject.new(id: 1, name: "no name2")
28
+
29
+ expect(first).not_to eq second
30
+ end
31
+ end
32
+
33
+ end
@@ -17,6 +17,17 @@ describe Yaoc::MappingToClass do
17
17
  }
18
18
 
19
19
  describe "#call" do
20
+ subject{
21
+ Struct.new(:target_source) do
22
+ include Yaoc::MappingToClass
23
+
24
+ self.mapping_strategy = ->(obj){
25
+ {:name => :new_name}
26
+ }
27
+
28
+ end.new(expected_class)
29
+ }
30
+
20
31
  it "creates on object of the wanted kind" do
21
32
  expect(subject.call).to be_kind_of expected_class
22
33
  end
@@ -29,7 +40,7 @@ describe Yaoc::MappingToClass do
29
40
  end
30
41
 
31
42
 
32
- it "splashes args when conversion result is an array" do
43
+ it "splattes args when conversion result is an array" do
33
44
  creator = ->(*args){}
34
45
  subject.class.mapping_strategy = ->(obj){
35
46
  [1, 2]
@@ -41,6 +52,15 @@ describe Yaoc::MappingToClass do
41
52
 
42
53
  subject.call
43
54
  end
55
+
56
+ it "fills an existing object instead of create a new one" do
57
+ obj = Struct.new(:id, :name).new(:my_id)
58
+ created_obj = subject.call(obj)
59
+
60
+ expect(created_obj).to eq obj
61
+ expect(obj.name).to eq :new_name
62
+ expect(obj.id).to eq :my_id
63
+ end
44
64
  end
45
65
 
46
66
  describe "#to_a" do
@@ -173,6 +173,14 @@ describe Yaoc::ObjectMapper do
173
173
 
174
174
  subject.load({})
175
175
  end
176
+
177
+ it "uses an existing object for the result" do
178
+ preloaded_obj = Object.new
179
+
180
+ expect(converter).to receive(:call).with(preloaded_obj)
181
+
182
+ subject.load({}, preloaded_obj)
183
+ end
176
184
  end
177
185
 
178
186
  describe "#dump" do
@@ -183,6 +191,14 @@ describe Yaoc::ObjectMapper do
183
191
  subject.dump({})
184
192
  end
185
193
 
194
+ it "uses an existing object for the result" do
195
+ preloaded_obj = Object.new
196
+
197
+ expect(reverse_converter).to receive(:call).with(preloaded_obj)
198
+
199
+ subject.dump({}, preloaded_obj)
200
+ end
201
+
186
202
  end
187
203
 
188
204
  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.5
4
+ version: 0.0.6
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-24 00:00:00.000000000 Z
11
+ date: 2014-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: abstract_type
@@ -214,19 +214,27 @@ extensions: []
214
214
  extra_rdoc_files: []
215
215
  files:
216
216
  - ".gitignore"
217
+ - ".travis.yml"
217
218
  - Gemfile
218
219
  - Guardfile
219
220
  - LICENSE.txt
220
221
  - README.md
221
222
  - Rakefile
223
+ - examples/01_hash_enabled_constructors.rb
224
+ - examples/02_procs_as_constructors.rb
225
+ - examples/03_positional_constructors.rb
226
+ - examples/04_compositions.rb
227
+ - examples/05_fill_existing_objects.rb
222
228
  - lib/yaoc.rb
223
229
  - lib/yaoc/converter_builder.rb
230
+ - lib/yaoc/helper/struct_hash_constructor.rb
224
231
  - lib/yaoc/mapping_base.rb
225
232
  - lib/yaoc/mapping_to_class.rb
226
233
  - lib/yaoc/object_mapper.rb
227
234
  - lib/yaoc/strategies/to_array_mapping.rb
228
235
  - lib/yaoc/strategies/to_hash_mapping.rb
229
236
  - lib/yaoc/version.rb
237
+ - spec/acceptance/fill_existing_objects_spec.rb
230
238
  - spec/acceptance/map_objects_spec.rb
231
239
  - spec/acceptance/map_to_objects_using_other_converters_spec.rb
232
240
  - spec/acceptance/map_to_objects_with_positional_constructors_spec.rb
@@ -234,6 +242,7 @@ files:
234
242
  - spec/spec_helper.rb
235
243
  - spec/support/feature.rb
236
244
  - spec/unit/lib/yaoc/converter_builder_spec.rb
245
+ - spec/unit/lib/yaoc/helper/struct_hash_constructor_spec.rb
237
246
  - spec/unit/lib/yaoc/mapping_base_spec.rb
238
247
  - spec/unit/lib/yaoc/mapping_to_class_spec.rb
239
248
  - spec/unit/lib/yaoc/object_mapper_spec.rb
@@ -265,6 +274,7 @@ signing_key:
265
274
  specification_version: 4
266
275
  summary: Yet another object converter
267
276
  test_files:
277
+ - spec/acceptance/fill_existing_objects_spec.rb
268
278
  - spec/acceptance/map_objects_spec.rb
269
279
  - spec/acceptance/map_to_objects_using_other_converters_spec.rb
270
280
  - spec/acceptance/map_to_objects_with_positional_constructors_spec.rb
@@ -272,6 +282,7 @@ test_files:
272
282
  - spec/spec_helper.rb
273
283
  - spec/support/feature.rb
274
284
  - spec/unit/lib/yaoc/converter_builder_spec.rb
285
+ - spec/unit/lib/yaoc/helper/struct_hash_constructor_spec.rb
275
286
  - spec/unit/lib/yaoc/mapping_base_spec.rb
276
287
  - spec/unit/lib/yaoc/mapping_to_class_spec.rb
277
288
  - spec/unit/lib/yaoc/object_mapper_spec.rb