yaoc 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -9
- data/examples/01_hash_enabled_constructors.rb +3 -3
- data/examples/02_procs_as_constructors.rb +3 -3
- data/examples/03_positional_constructors.rb +12 -2
- data/lib/yaoc.rb +5 -2
- data/lib/yaoc/converter_builder.rb +16 -13
- data/lib/yaoc/helper/to_proc_delegator.rb +2 -0
- data/lib/yaoc/mapping_base.rb +2 -30
- data/lib/yaoc/object_mapper.rb +11 -2
- data/lib/yaoc/transformation_command.rb +53 -0
- data/lib/yaoc/transformation_deferred_command.rb +17 -0
- data/lib/yaoc/version.rb +1 -1
- data/spec/acceptance/map_objects_spec.rb +2 -2
- data/spec/acceptance/map_to_objects_using_other_converters_spec.rb +1 -0
- data/spec/unit/lib/yaoc/converter_builder_spec.rb +17 -0
- data/spec/unit/lib/yaoc/helper/to_proc_delegator_spec.rb +31 -0
- data/spec/unit/lib/yaoc/mapping_base_spec.rb +2 -22
- data/spec/unit/lib/yaoc/object_mapper_spec.rb +29 -0
- data/spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb +1 -1
- data/spec/unit/lib/yaoc/strategies/to_hash_mapping_spec.rb +1 -1
- data/spec/unit/lib/yaoc/transformation_command_spec.rb +70 -0
- data/spec/unit/lib/yaoc/transformation_deferred_command_spec.rb +32 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2324f744d79c8b4447ce737b2eb4530c2ea22e49
|
4
|
+
data.tar.gz: 4f455b5e33867e51b06c248896f42eb24b5bac70
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8292da0e6c0ccfd4259c457c10da22351df4cc7b9081ada86cdf6ea3945ad0e69bccd7a26767243176193c9a0b7a0585be9f4c60c3886ff2e30005c8852b2cbc
|
7
|
+
data.tar.gz: 5744c15c87cdf05a21fef126b4747a94a23100d6dd82287cb6ac56058068a28750e45284537130b1aa146c35cdad293f6b0fb9658958c5485c1d09e24145ba8d
|
data/README.md
CHANGED
@@ -18,7 +18,7 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
For uptodate doc's take a look into the specs.
|
22
22
|
|
23
23
|
### The resulting classes have hash enabled constructors?
|
24
24
|
|
@@ -39,12 +39,12 @@ mapper = Yaoc::ObjectMapper.new(User, OldUser).tap do |mapper|
|
|
39
39
|
|
40
40
|
rule to: :firstname,
|
41
41
|
from: :fullname,
|
42
|
-
converter: ->(source, result){ fill_result_with_value(result, :firstname, source.fullname.split().first) },
|
43
|
-
reverse_converter: ->(source, result){ fill_result_with_value(result, :fullname, "#{source.firstname} #{source.lastname}") }
|
42
|
+
converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :firstname, source.fullname.split().first) },
|
43
|
+
reverse_converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :fullname, "#{source.firstname} #{source.lastname}") }
|
44
44
|
|
45
45
|
rule to: :lastname,
|
46
46
|
from: :fullname,
|
47
|
-
converter: ->(source, result){ fill_result_with_value(result, :lastname, source.fullname.split().last ) },
|
47
|
+
converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :lastname, source.fullname.split().last ) },
|
48
48
|
reverse_converter: ->(source, result){ result }
|
49
49
|
|
50
50
|
rule to: :id
|
@@ -104,12 +104,12 @@ mapper = Yaoc::ObjectMapper.new(source, reverse_source).tap do |mapper|
|
|
104
104
|
|
105
105
|
rule to: :firstname,
|
106
106
|
from: :fullname,
|
107
|
-
converter: ->(source, result){ fill_result_with_value(result, :firstname, source.fullname.split().first ) },
|
108
|
-
reverse_converter: ->(source, result){ fill_result_with_value(result, :fullname, "#{source.firstname} #{source.lastname}") }
|
107
|
+
converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :firstname, source.fullname.split().first ) },
|
108
|
+
reverse_converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :fullname, "#{source.firstname} #{source.lastname}") }
|
109
109
|
|
110
110
|
rule to: :lastname,
|
111
111
|
from: :fullname,
|
112
|
-
converter: ->(source, result){ fill_result_with_value(result, :lastname, source.fullname.split().last) },
|
112
|
+
converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :lastname, source.fullname.split().last) },
|
113
113
|
reverse_converter: ->(source, result){ result }
|
114
114
|
|
115
115
|
rule to: :id
|
@@ -147,6 +147,16 @@ puts "\n" * 5
|
|
147
147
|
OldUser3 = Struct.new(:id, :fullname, :r_role)
|
148
148
|
User3 = Struct.new(:id, :firstname, :lastname, :role)
|
149
149
|
|
150
|
+
# alternative to proc for converter
|
151
|
+
converter = Yaoc::TransformationCommand.create(to: 1,
|
152
|
+
from: :fullname,
|
153
|
+
deferred: false,
|
154
|
+
fetcher_proc: ->(source, fetcher, from){source.fullname.split().first} )
|
155
|
+
|
156
|
+
reverse_converter = Yaoc::TransformationCommand.create(to: 1,
|
157
|
+
from: :first_and_lastname,
|
158
|
+
deferred: false,
|
159
|
+
fetcher_proc: ->(source, fetcher, from){ "#{source.firstname} #{source.lastname}"} )
|
150
160
|
|
151
161
|
mapper = Yaoc::ObjectMapper.new(User3, OldUser3).tap do |mapper|
|
152
162
|
mapper.add_mapping do
|
@@ -161,8 +171,8 @@ mapper = Yaoc::ObjectMapper.new(User3, OldUser3).tap do |mapper|
|
|
161
171
|
rule to: 1,
|
162
172
|
from: :fullname,
|
163
173
|
|
164
|
-
converter:
|
165
|
-
reverse_converter:
|
174
|
+
converter: converter,
|
175
|
+
reverse_converter: reverse_converter
|
166
176
|
|
167
177
|
rule to: 2,
|
168
178
|
from: :fullname,
|
@@ -187,6 +197,7 @@ new_user3.lastname = "name"
|
|
187
197
|
|
188
198
|
puts mapper.dump(new_user3)
|
189
199
|
|
200
|
+
|
190
201
|
#<struct OldUser3 id=1, fullname="myfirst mysecond", r_role="admin">
|
191
202
|
#<struct User3 id=1, firstname="myfirst", lastname="mysecond", role="admin">
|
192
203
|
#<struct OldUser3 id=1, fullname="no name", r_role="admin">
|
@@ -16,12 +16,12 @@ mapper = Yaoc::ObjectMapper.new(User, OldUser).tap do |mapper|
|
|
16
16
|
|
17
17
|
rule to: :firstname,
|
18
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}") }
|
19
|
+
converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :firstname, source.fullname.split().first) },
|
20
|
+
reverse_converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :fullname, "#{source.firstname} #{source.lastname}") }
|
21
21
|
|
22
22
|
rule to: :lastname,
|
23
23
|
from: :fullname,
|
24
|
-
converter: ->(source, result){ fill_result_with_value(result, :lastname, source.fullname.split().last ) },
|
24
|
+
converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :lastname, source.fullname.split().last ) },
|
25
25
|
reverse_converter: ->(source, result){ result }
|
26
26
|
|
27
27
|
rule to: :id
|
@@ -34,12 +34,12 @@ mapper = Yaoc::ObjectMapper.new(source, reverse_source).tap do |mapper|
|
|
34
34
|
|
35
35
|
rule to: :firstname,
|
36
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}") }
|
37
|
+
converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :firstname, source.fullname.split().first ) },
|
38
|
+
reverse_converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :fullname, "#{source.firstname} #{source.lastname}") }
|
39
39
|
|
40
40
|
rule to: :lastname,
|
41
41
|
from: :fullname,
|
42
|
-
converter: ->(source, result){ fill_result_with_value(result, :lastname, source.fullname.split().last) },
|
42
|
+
converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :lastname, source.fullname.split().last) },
|
43
43
|
reverse_converter: ->(source, result){ result }
|
44
44
|
|
45
45
|
rule to: :id
|
@@ -11,6 +11,16 @@ OldUser3 = Struct.new(:id, :fullname, :r_role)
|
|
11
11
|
User3 = Struct.new(:id, :firstname, :lastname, :role)
|
12
12
|
|
13
13
|
|
14
|
+
converter = Yaoc::TransformationCommand.create(to: 1,
|
15
|
+
from: :fullname,
|
16
|
+
deferred: false,
|
17
|
+
fetcher_proc: ->(source, fetcher, from){source.fullname.split().first} )
|
18
|
+
|
19
|
+
reverse_converter = Yaoc::TransformationCommand.create(to: 1,
|
20
|
+
from: :first_and_lastname,
|
21
|
+
deferred: false,
|
22
|
+
fetcher_proc: ->(source, fetcher, from){ "#{source.firstname} #{source.lastname}"} )
|
23
|
+
|
14
24
|
mapper = Yaoc::ObjectMapper.new(User3, OldUser3).tap do |mapper|
|
15
25
|
mapper.add_mapping do
|
16
26
|
fetcher :public_send
|
@@ -24,8 +34,8 @@ mapper = Yaoc::ObjectMapper.new(User3, OldUser3).tap do |mapper|
|
|
24
34
|
rule to: 1,
|
25
35
|
from: :fullname,
|
26
36
|
|
27
|
-
converter:
|
28
|
-
reverse_converter:
|
37
|
+
converter: converter,
|
38
|
+
reverse_converter: reverse_converter
|
29
39
|
|
30
40
|
rule to: 2,
|
31
41
|
from: :fullname,
|
data/lib/yaoc.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
require "yaoc/version"
|
2
|
+
|
3
|
+
Dir[File.join(File.expand_path(__dir__ ), "yaoc/helper/*.rb")].each { |f| require f }
|
4
|
+
|
2
5
|
require 'yaoc/mapping_base'
|
6
|
+
require 'yaoc/transformation_command'
|
7
|
+
require 'yaoc/transformation_deferred_command'
|
3
8
|
require 'yaoc/mapping_to_class'
|
4
9
|
|
5
10
|
Dir[File.join(File.expand_path(__dir__ ), "yaoc/strategies/*.rb")].each { |f| require f }
|
@@ -7,8 +12,6 @@ Dir[File.join(File.expand_path(__dir__ ), "yaoc/strategies/*.rb")].each { |f| re
|
|
7
12
|
require 'yaoc/converter_builder'
|
8
13
|
require 'yaoc/object_mapper'
|
9
14
|
|
10
|
-
Dir[File.join(File.expand_path(__dir__ ), "yaoc/helper/*.rb")].each { |f| require f }
|
11
|
-
|
12
15
|
module Yaoc
|
13
16
|
|
14
17
|
end
|
@@ -36,20 +36,19 @@ module Yaoc
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def converter_to_proc(to, from, converter, is_collection, deferred)
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
}
|
50
|
-
|
51
|
-
fill_result_from_proc(result, to, get_value_with, deferred)
|
39
|
+
|
40
|
+
get_value_with = ->(source, fetcher, from){
|
41
|
+
object_to_convert = source.public_send(fetcher, from)
|
42
|
+
|
43
|
+
if is_collection
|
44
|
+
object_to_convert.map(&converter)
|
45
|
+
else
|
46
|
+
converter_as_proc = converter.to_proc
|
47
|
+
converter_as_proc.call(object_to_convert)
|
48
|
+
end
|
52
49
|
}
|
50
|
+
|
51
|
+
TransformationCommand.create(to: to, from: from, deferred: deferred, fetcher_proc: get_value_with)
|
53
52
|
end
|
54
53
|
|
55
54
|
end
|
@@ -83,6 +82,10 @@ module Yaoc
|
|
83
82
|
build_commands.reverse
|
84
83
|
end
|
85
84
|
end
|
85
|
+
|
86
|
+
def noop
|
87
|
+
->(_, result){ result }
|
88
|
+
end
|
86
89
|
end
|
87
90
|
|
88
91
|
|
data/lib/yaoc/mapping_base.rb
CHANGED
@@ -25,25 +25,6 @@ module Yaoc
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
def fill_result_with_value(result, key, value)
|
29
|
-
result.tap{|taped_result| taped_result[key] = value}
|
30
|
-
end
|
31
|
-
|
32
|
-
def fill_result_from_proc(result, key, proc, deferred=false)
|
33
|
-
value = if deferred
|
34
|
-
deferrer_strategy(proc)
|
35
|
-
else
|
36
|
-
proc.call
|
37
|
-
end
|
38
|
-
|
39
|
-
fill_result_with_value(result, key, value)
|
40
|
-
end
|
41
|
-
|
42
|
-
def deferrer_strategy(proc)
|
43
|
-
Yaoc::Helper::ToProcDelegator.new(proc)
|
44
|
-
end
|
45
|
-
|
46
|
-
|
47
28
|
def converter_methods
|
48
29
|
self.class.converter_methods
|
49
30
|
end
|
@@ -54,16 +35,6 @@ module Yaoc
|
|
54
35
|
|
55
36
|
module ClassMethods
|
56
37
|
|
57
|
-
def converter_proc(to, from, deferred=false)
|
58
|
-
-> (to_convert, result){
|
59
|
-
get_value_with = ->{
|
60
|
-
to_convert.public_send(fetcher, from)
|
61
|
-
}
|
62
|
-
|
63
|
-
fill_result_from_proc(result, to, get_value_with, deferred)
|
64
|
-
}
|
65
|
-
end
|
66
|
-
|
67
38
|
def mapping_strategy=(new_strat)
|
68
39
|
@mapping_strategy = new_strat
|
69
40
|
end
|
@@ -74,7 +45,7 @@ module Yaoc
|
|
74
45
|
|
75
46
|
def map(to: nil, from: to, converter: nil, lazy_loading: false)
|
76
47
|
class_private_module(:Mapping).tap do |mod|
|
77
|
-
method_implementation =
|
48
|
+
method_implementation = TransformationCommand.create(to: to, from: from, deferred: lazy_loading, conversion_proc: converter)
|
78
49
|
|
79
50
|
mod.send :define_method, "map_#{"%04d" %[converter_methods.count]}_#{from}_to_#{to}".to_sym, method_implementation
|
80
51
|
include mod
|
@@ -105,5 +76,6 @@ module Yaoc
|
|
105
76
|
end
|
106
77
|
|
107
78
|
end
|
79
|
+
|
108
80
|
end
|
109
81
|
end
|
data/lib/yaoc/object_mapper.rb
CHANGED
@@ -6,16 +6,20 @@ module Yaoc
|
|
6
6
|
apply_commands!
|
7
7
|
end
|
8
8
|
|
9
|
-
def rule(to: nil,
|
9
|
+
def rule(to: nil,
|
10
|
+
from: to,
|
11
|
+
converter: nil,
|
10
12
|
reverse_to: from,
|
11
13
|
reverse_from: to,
|
12
14
|
reverse_converter: nil,
|
13
15
|
object_converter: nil,
|
16
|
+
reverse_object_converter: object_converter,
|
14
17
|
is_collection: nil,
|
15
18
|
lazy_loading: nil,
|
16
19
|
reverse_lazy_loading: lazy_loading)
|
17
20
|
|
18
21
|
object_converter = Array(object_converter)
|
22
|
+
reverse_object_converter = Array(reverse_object_converter)
|
19
23
|
|
20
24
|
converter_builder.rule(
|
21
25
|
to: to,
|
@@ -30,7 +34,7 @@ module Yaoc
|
|
30
34
|
to: reverse_to,
|
31
35
|
from: reverse_from,
|
32
36
|
converter: reverse_converter,
|
33
|
-
object_converter:
|
37
|
+
object_converter: reverse_object_converter.map(&:reverse_converter),
|
34
38
|
is_collection: is_collection,
|
35
39
|
lazy_loading: reverse_lazy_loading
|
36
40
|
)
|
@@ -51,6 +55,11 @@ module Yaoc
|
|
51
55
|
def reverse_strategy(new_strategy)
|
52
56
|
reverse_converter_builder.strategy = new_strategy
|
53
57
|
end
|
58
|
+
|
59
|
+
def noop
|
60
|
+
->(_, result){ result }
|
61
|
+
end
|
62
|
+
|
54
63
|
end
|
55
64
|
|
56
65
|
class ObjectMapper
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Yaoc
|
2
|
+
class TransformationCommand
|
3
|
+
|
4
|
+
protected
|
5
|
+
attr_accessor :to, :from, :fetcher , :proc, :value_fetcher_proc
|
6
|
+
|
7
|
+
public
|
8
|
+
|
9
|
+
def self.deferred_source
|
10
|
+
TransformationDeferredCommand
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.default_source
|
14
|
+
TransformationCommand
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.create(to: nil, from: nil, deferred: false, conversion_proc: nil, fetcher_proc: nil)
|
18
|
+
# will be executed in mapper object instance context later through :define_method
|
19
|
+
tc_source = deferred ? deferred_source : default_source
|
20
|
+
|
21
|
+
-> (to_convert, result){
|
22
|
+
tc_source.new(to: to, from: from, fetch_method: fetcher, conversion_proc: conversion_proc, fetcher_proc: fetcher_proc).call(to_convert, result)
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.fill_result_with_value(result, key, value)
|
27
|
+
result.tap{|taped_result| taped_result[key] = value}
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(to: nil, from: nil, fetch_method: nil, conversion_proc: nil, fetcher_proc: nil)
|
31
|
+
self.to = to
|
32
|
+
self.from = from
|
33
|
+
self.proc = conversion_proc
|
34
|
+
self.fetcher = fetch_method
|
35
|
+
self.value_fetcher_proc = fetcher_proc || ->(to_convert, fetcher, from){ to_convert.public_send(fetcher, from)}
|
36
|
+
end
|
37
|
+
|
38
|
+
def call(to_convert, result)
|
39
|
+
|
40
|
+
unless proc.nil?
|
41
|
+
instance_exec(to_convert, result, &proc)
|
42
|
+
else
|
43
|
+
TransformationCommand.fill_result_with_value(result, to, value(to_convert))
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
def value(to_convert)
|
49
|
+
value_fetcher_proc.call(to_convert, fetcher, from)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Yaoc
|
2
|
+
class TransformationDeferredCommand < TransformationCommand
|
3
|
+
|
4
|
+
def value(time_saved_to_convert)
|
5
|
+
proc = ->{
|
6
|
+
value_fetcher_proc.call(time_saved_to_convert, fetcher, from)
|
7
|
+
}
|
8
|
+
|
9
|
+
TransformationDeferredCommand.deferrer_strategy(proc)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.deferrer_strategy(proc)
|
13
|
+
Yaoc::Helper::ToProcDelegator.new(proc)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
data/lib/yaoc/version.rb
CHANGED
@@ -12,8 +12,8 @@ feature "Map objects", %q{
|
|
12
12
|
fetcher :public_send
|
13
13
|
reverse_fetcher :public_send
|
14
14
|
rule to: :name,
|
15
|
-
converter: ->(source, result){ fill_result_with_value(result, :name, "#{source[:name]} Hello World") },
|
16
|
-
reverse_converter: ->(source, result){ fill_result_with_value(result, :name, source.name) }
|
15
|
+
converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :name, "#{source[:name]} Hello World") },
|
16
|
+
reverse_converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :name, source.name) }
|
17
17
|
rule to: :role, from: :fullrolename
|
18
18
|
rule to: :id
|
19
19
|
rule to: [:foo, :bar]
|
@@ -161,6 +161,17 @@ describe Yaoc::ConverterBuilder do
|
|
161
161
|
lazy_loading: true
|
162
162
|
end
|
163
163
|
end
|
164
|
+
|
165
|
+
it "supports a do nothing" do
|
166
|
+
expected_args = default_map_args.clone.merge({converter: kind_of(Proc)})
|
167
|
+
|
168
|
+
expect(converter_class).to receive(:map).ordered.with(expected_args)
|
169
|
+
|
170
|
+
subject.add_mapping do
|
171
|
+
rule to: :id,
|
172
|
+
converter: noop
|
173
|
+
end
|
174
|
+
end
|
164
175
|
end
|
165
176
|
|
166
177
|
describe "#converter" do
|
@@ -181,4 +192,10 @@ describe Yaoc::ConverterBuilder do
|
|
181
192
|
end
|
182
193
|
end
|
183
194
|
|
195
|
+
describe "#noop" do
|
196
|
+
it "returns the input" do
|
197
|
+
expect(subject.noop.call(:some_thing, :expected_value)).to eq :expected_value
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
184
201
|
end
|
@@ -41,6 +41,12 @@ describe Yaoc::Helper::ToProcDelegator do
|
|
41
41
|
|
42
42
|
end
|
43
43
|
|
44
|
+
describe '#class' do
|
45
|
+
it 'is the lazy loaded object class' do
|
46
|
+
expect(subject.class).to eq Array
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
44
50
|
describe '#nil?' do
|
45
51
|
it "returns true when delegate value is nil" do
|
46
52
|
subject = Yaoc::Helper::ToProcDelegator.new(->{nil})
|
@@ -52,4 +58,29 @@ describe Yaoc::Helper::ToProcDelegator do
|
|
52
58
|
end
|
53
59
|
end
|
54
60
|
|
61
|
+
describe '#_initialisation_proc_loaded?' do
|
62
|
+
it "returns true, when __getobj__ was accessed" do
|
63
|
+
expect(subject._initialisation_proc_loaded?).to be_falsy
|
64
|
+
subject.__getobj__
|
65
|
+
expect(subject._initialisation_proc_loaded?).to be_truthy
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '_needs_conversion?' do
|
70
|
+
it 'returns true when the object was loaded and __getobj__ is not nil' do
|
71
|
+
subject.__getobj__
|
72
|
+
expect(subject._needs_conversion?).to be_truthy
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'returns false when the object was not' do
|
76
|
+
expect(subject._needs_conversion?).to be_falsy
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'returns false when the object was loaded but is nil' do
|
80
|
+
subject.__getobj__
|
81
|
+
subject.__setobj__(nil)
|
82
|
+
expect(subject._needs_conversion?).to be_falsy
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
55
86
|
end
|
@@ -32,31 +32,11 @@ describe Yaoc::MappingBase do
|
|
32
32
|
{:foo=>:my_to_convert, :bar=>:my_result}]
|
33
33
|
end
|
34
34
|
|
35
|
-
it "
|
35
|
+
it "delegates to TransformationCommand.create" do
|
36
|
+
expect(Yaoc::TransformationCommand).to receive(:create).with(to: :bar, from: :foo, deferred: false, conversion_proc: kind_of(Proc)).and_return(->(*){})
|
36
37
|
subject.map(to: :bar, from: :foo, converter: ->(*){})
|
37
|
-
|
38
|
-
expect(subject.new(:my_to_convert).call()).to eq [nil]
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'supports deferred mappings' do
|
42
|
-
subject.map(to: :bar, from: :foo, lazy_loading: true)
|
43
|
-
object_to_convert = {foo: [:my_result]}
|
44
|
-
|
45
|
-
expect(object_to_convert).not_to receive :fetch
|
46
|
-
|
47
|
-
subject.new(object_to_convert).call()
|
48
38
|
end
|
49
39
|
|
50
|
-
it "returns results for deferred mappings" do
|
51
|
-
subject.map(to: :bar, from: :foo, lazy_loading: true)
|
52
|
-
object_to_convert = double("object_to_convert")
|
53
|
-
|
54
|
-
result = subject.new(object_to_convert).call()
|
55
|
-
|
56
|
-
expect(object_to_convert).to receive(:fetch).with(:foo).and_return([:my_result])
|
57
|
-
|
58
|
-
expect(result.first[:bar].first).to eq :my_result
|
59
|
-
end
|
60
40
|
end
|
61
41
|
|
62
42
|
describe "#converter_methods" do
|
@@ -88,6 +88,29 @@ describe Yaoc::ObjectMapper do
|
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
+
it "allows to use another converter as reverse converter" do
|
92
|
+
reverse_converter_double = double("reverse converter")
|
93
|
+
|
94
|
+
expected_params = expected_default_params.merge(
|
95
|
+
object_converter: [],
|
96
|
+
)
|
97
|
+
|
98
|
+
expected_params_reverse = expected_default_params.merge(
|
99
|
+
object_converter: [reverse_converter_double]
|
100
|
+
)
|
101
|
+
|
102
|
+
|
103
|
+
expect(converter_builder).to receive(:rule).with(expected_params)
|
104
|
+
expect(reverse_converter_builder).to receive(:rule).with(expected_params_reverse)
|
105
|
+
|
106
|
+
expect(reverse_converter_double).to receive(:reverse_converter).and_return(reverse_converter_double)
|
107
|
+
|
108
|
+
subject.add_mapping do
|
109
|
+
rule to: :id,
|
110
|
+
reverse_object_converter: reverse_converter_double
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
91
114
|
it "accepts a reverse mapping for from and to" do
|
92
115
|
expected_params = expected_default_params.merge(
|
93
116
|
to: :id_r,
|
@@ -207,4 +230,10 @@ describe Yaoc::ObjectMapper do
|
|
207
230
|
|
208
231
|
end
|
209
232
|
|
233
|
+
describe "#noop" do
|
234
|
+
it "returns the input" do
|
235
|
+
expect(subject.noop.call(:some_thing, :expected_value)).to eq :expected_value
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
210
239
|
end
|
@@ -32,7 +32,7 @@ describe Yaoc::Strategies::ToArrayMapping do
|
|
32
32
|
|
33
33
|
it "uses my converter proc" do
|
34
34
|
subject.map(to: 0, from: :id)
|
35
|
-
subject.map(to: 3, from: :fullname, converter: ->(source, result){ fill_result_with_value(result, 3, "#{source.fetch(:name)} Hello World") })
|
35
|
+
subject.map(to: 3, from: :fullname, converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, 3, "#{source.fetch(:name)} Hello World") })
|
36
36
|
|
37
37
|
ext_expectation = expected_array.clone
|
38
38
|
ext_expectation[3] = "#{ext_expectation[1]} Hello World"
|
@@ -41,7 +41,7 @@ describe Yaoc::Strategies::ToHashMapping do
|
|
41
41
|
|
42
42
|
it "uses my converter proc" do
|
43
43
|
subject.map(to: :id)
|
44
|
-
subject.map(to: :name, from: :fullname, converter: ->(source, result){ fill_result_with_value(result, :name, source.fetch(:name) + " Hello World") })
|
44
|
+
subject.map(to: :name, from: :fullname, converter: ->(source, result){ Yaoc::TransformationCommand.fill_result_with_value(result, :name, source.fetch(:name) + " Hello World") })
|
45
45
|
|
46
46
|
ext_expectation = expected_hash.clone
|
47
47
|
ext_expectation[:name] += " Hello World"
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Yaoc::TransformationCommand do
|
4
|
+
subject{
|
5
|
+
Yaoc::TransformationCommand.new(to: :id, from: :name, fetch_method: :fetch)
|
6
|
+
}
|
7
|
+
|
8
|
+
let(:source){
|
9
|
+
{name: 'my_name'}
|
10
|
+
}
|
11
|
+
|
12
|
+
let(:result){
|
13
|
+
{}
|
14
|
+
}
|
15
|
+
|
16
|
+
describe '.create' do
|
17
|
+
subject{
|
18
|
+
Yaoc::TransformationCommand
|
19
|
+
}
|
20
|
+
|
21
|
+
it 'creates a proc' do
|
22
|
+
expect(subject.create(to: :to, from: :from)).to respond_to :call
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'uses default_source when deferred is flase' do
|
26
|
+
expect(subject).to receive :default_source
|
27
|
+
subject.create(to: :to, from: :from)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'uses default_source when deferred is flase' do
|
31
|
+
expect(subject).to receive :deferred_source
|
32
|
+
subject.create(to: :to, from: :from, deferred: true)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#call' do
|
38
|
+
|
39
|
+
it 'transfers data from source to result' do
|
40
|
+
subject.call(source, result)
|
41
|
+
expect(result[:id]).to eq source[:name]
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'add the fetched value to result' do
|
45
|
+
new_result = subject.call(source, result)
|
46
|
+
expect(new_result).to eq result
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'returns a result' do
|
50
|
+
new_result = subject.call(source, result)
|
51
|
+
expect(new_result).to eq result
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#value' do
|
57
|
+
subject {
|
58
|
+
Yaoc::TransformationCommand.new(to: :id, from: :name, fetch_method: :fetch, fetcher_proc: value_fetcher)
|
59
|
+
}
|
60
|
+
|
61
|
+
let(:value_fetcher) { double('value fetcher proc')}
|
62
|
+
|
63
|
+
it 'uses a given proc for value fetching' do
|
64
|
+
expect(value_fetcher).to receive(:call).with(source, :fetch, :name).and_return(:some_thing_to_store)
|
65
|
+
|
66
|
+
expect(subject.value(source)).to eq :some_thing_to_store
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Yaoc::TransformationDeferredCommand do
|
4
|
+
subject{
|
5
|
+
Yaoc::TransformationDeferredCommand.new(to: :id, from: :name, fetch_method: :fetch)
|
6
|
+
}
|
7
|
+
|
8
|
+
let(:source){
|
9
|
+
{name: 'my_name'}
|
10
|
+
}
|
11
|
+
|
12
|
+
let(:result){
|
13
|
+
{}
|
14
|
+
}
|
15
|
+
|
16
|
+
describe '#value' do
|
17
|
+
let(:value_fetcher) { double('value fetcher proc')}
|
18
|
+
|
19
|
+
it 'deferres access to source object' do
|
20
|
+
expect(source).not_to receive :fetch
|
21
|
+
|
22
|
+
subject.value(source)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'access to source object, when data is needed' do
|
26
|
+
expect(source).to receive :fetch
|
27
|
+
|
28
|
+
subject.value(source).to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
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.9
|
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-
|
11
|
+
date: 2014-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -222,6 +222,8 @@ files:
|
|
222
222
|
- lib/yaoc/object_mapper.rb
|
223
223
|
- lib/yaoc/strategies/to_array_mapping.rb
|
224
224
|
- lib/yaoc/strategies/to_hash_mapping.rb
|
225
|
+
- lib/yaoc/transformation_command.rb
|
226
|
+
- lib/yaoc/transformation_deferred_command.rb
|
225
227
|
- lib/yaoc/version.rb
|
226
228
|
- spec/acceptance/fill_existing_objects_spec.rb
|
227
229
|
- spec/acceptance/map_objects_spec.rb
|
@@ -239,6 +241,8 @@ files:
|
|
239
241
|
- spec/unit/lib/yaoc/object_mapper_spec.rb
|
240
242
|
- spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb
|
241
243
|
- spec/unit/lib/yaoc/strategies/to_hash_mapping_spec.rb
|
244
|
+
- spec/unit/lib/yaoc/transformation_command_spec.rb
|
245
|
+
- spec/unit/lib/yaoc/transformation_deferred_command_spec.rb
|
242
246
|
- yaoc.gemspec
|
243
247
|
homepage: https://github.com/slowjack2k/yaoc
|
244
248
|
licenses:
|
@@ -281,3 +285,5 @@ test_files:
|
|
281
285
|
- spec/unit/lib/yaoc/object_mapper_spec.rb
|
282
286
|
- spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb
|
283
287
|
- spec/unit/lib/yaoc/strategies/to_hash_mapping_spec.rb
|
288
|
+
- spec/unit/lib/yaoc/transformation_command_spec.rb
|
289
|
+
- spec/unit/lib/yaoc/transformation_deferred_command_spec.rb
|