yaoc 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -2
- data/lib/yaoc/object_mapper.rb +25 -9
- data/lib/yaoc/version.rb +1 -1
- data/spec/acceptance/map_objects_spec.rb +29 -10
- data/spec/unit/lib/yaoc/object_mapper_spec.rb +9 -5
- data/yaoc.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa280d846afba6ccd2f33412e0ff29851e644456
|
4
|
+
data.tar.gz: 9db2caf9165fafc3225b647d9a7fd504dc302a1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 002631bd391b6fe657556f7c2c80c0289fb5ab26cb04e78c8593abc9b1e1288e80d2646ea8b63433d9ca20cb4bed2d4e7a477be0fe26dcce757b8363f5981f51
|
7
|
+
data.tar.gz: 08ef26547007bd532debdcbd39045de8d61e96adc59d9b812329d2b5e3b4ebeb408df5e3137cc0e76866d8667bb461d89c1b7c728c86da9f2a5a8ab949d2973a
|
data/README.md
CHANGED
@@ -22,6 +22,8 @@ Uptodate doc's look into the specs.
|
|
22
22
|
|
23
23
|
```ruby
|
24
24
|
|
25
|
+
require 'yaoc'
|
26
|
+
|
25
27
|
User = Struct.new(:id, :name) do
|
26
28
|
def initialize(params={})
|
27
29
|
super()
|
@@ -32,16 +34,38 @@ User = Struct.new(:id, :name) do
|
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
35
|
-
|
37
|
+
OldUser = Struct.new(:id, :fullname) do
|
38
|
+
def initialize(params={})
|
39
|
+
super()
|
40
|
+
|
41
|
+
params.each do |attr, value|
|
42
|
+
self.public_send("#{attr}=", value)
|
43
|
+
end if params
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
mapper = Yaoc::ObjectMapper.new(User, OldUser).tap do |mapper|
|
36
48
|
mapper.add_mapping do
|
49
|
+
fetcher :public_send
|
37
50
|
rule to: :name, from: :fullname
|
38
51
|
rule to: :id
|
39
52
|
end
|
40
53
|
end
|
41
54
|
|
55
|
+
old_user = OldUser.new({id: 1, fullname: "myname" })
|
56
|
+
new_user = mapper.load(old_user)
|
57
|
+
|
58
|
+
|
59
|
+
puts old_user
|
60
|
+
puts new_user
|
61
|
+
|
62
|
+
new_user.name = "no name"
|
42
63
|
|
43
|
-
|
64
|
+
puts mapper.dump(new_user)
|
44
65
|
|
66
|
+
#<struct OldUser id=1, fullname="myname">
|
67
|
+
#<struct User id=1, name="myname">
|
68
|
+
#<struct OldUser id=1, fullname="no name">
|
45
69
|
|
46
70
|
```
|
47
71
|
|
data/lib/yaoc/object_mapper.rb
CHANGED
@@ -1,31 +1,47 @@
|
|
1
1
|
module Yaoc
|
2
2
|
|
3
3
|
class ObjectMapper
|
4
|
-
attr_accessor :
|
4
|
+
attr_accessor :load_result_source, :dump_result_source,
|
5
|
+
:forward_commands, :backward_commads
|
5
6
|
|
6
|
-
def initialize(
|
7
|
-
self.
|
7
|
+
def initialize(load_result_source, dump_result_source=->(attrs){ attrs})
|
8
|
+
self.load_result_source = load_result_source.respond_to?(:call) ? load_result_source : ->(attrs){load_result_source.new(attrs)}
|
9
|
+
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 = []
|
8
13
|
end
|
9
14
|
|
10
15
|
def load(fetch_able)
|
11
|
-
call
|
12
|
-
|
13
|
-
result_class.new(call)
|
16
|
+
load_result_source.call(converter(fetch_able).call())
|
14
17
|
end
|
15
18
|
|
16
19
|
def dump(object)
|
17
|
-
reverse_converter(object).call()
|
20
|
+
dump_result_source.call(reverse_converter(object).call())
|
18
21
|
end
|
19
22
|
|
20
23
|
def add_mapping(&block)
|
21
24
|
instance_eval &block
|
25
|
+
apply_commands
|
22
26
|
end
|
23
27
|
|
24
28
|
protected
|
25
29
|
|
30
|
+
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
|
40
|
+
end
|
41
|
+
|
26
42
|
def rule(to: nil, from: to, converter: nil, reverse_converter: nil)
|
27
|
-
converter_class.map(to, from, converter)
|
28
|
-
reverse_converter_class.map(from, to, reverse_converter)
|
43
|
+
forward_commands.push ->{ converter_class.map(to, from, converter) }
|
44
|
+
backward_commads.unshift ->{ reverse_converter_class.map(from, to, reverse_converter) }
|
29
45
|
end
|
30
46
|
|
31
47
|
def fetcher(new_fetcher)
|
data/lib/yaoc/version.rb
CHANGED
@@ -7,9 +7,9 @@ feature "Map objects", %q{
|
|
7
7
|
} do
|
8
8
|
|
9
9
|
given(:mapper){
|
10
|
-
Yaoc::ObjectMapper.new(
|
10
|
+
Yaoc::ObjectMapper.new(load_result_object_class, dump_result_object_class).tap do |mapper|
|
11
11
|
mapper.add_mapping do
|
12
|
-
fetcher :
|
12
|
+
fetcher :public_send
|
13
13
|
reverse_fetcher :public_send
|
14
14
|
rule to: :name,
|
15
15
|
converter: ->(source, result){ result.merge({name: "#{source[:name]} Hello World"}) },
|
@@ -20,7 +20,7 @@ feature "Map objects", %q{
|
|
20
20
|
end
|
21
21
|
}
|
22
22
|
|
23
|
-
given(:
|
23
|
+
given(:load_result_object_class) {
|
24
24
|
Struct.new(:id, :name, :role) do
|
25
25
|
include Equalizer.new(:id, :name, :role)
|
26
26
|
|
@@ -35,22 +35,41 @@ feature "Map objects", %q{
|
|
35
35
|
end
|
36
36
|
}
|
37
37
|
|
38
|
+
given(:dump_result_object_class) {
|
39
|
+
Struct.new(:id, :name, :fullrolename) do
|
40
|
+
include Equalizer.new(:id, :name, :fullrolename)
|
41
|
+
|
42
|
+
def initialize(params={})
|
43
|
+
super()
|
44
|
+
|
45
|
+
params.each do |attr, value|
|
46
|
+
self.public_send("#{attr}=", value)
|
47
|
+
end if params
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
}
|
52
|
+
|
38
53
|
given(:input_hash){
|
39
|
-
|
54
|
+
dump_result_object
|
55
|
+
}
|
56
|
+
|
57
|
+
given(:load_result_object){
|
58
|
+
load_result_object_class.new({id: 1, name: "paul", role: "admin"})
|
40
59
|
}
|
41
60
|
|
42
|
-
given(:
|
43
|
-
|
61
|
+
given(:dump_result_object){
|
62
|
+
dump_result_object_class.new({id: 1, name: "paul", fullrolename: "admin"})
|
44
63
|
}
|
45
64
|
|
46
65
|
scenario "creates an result object from an input_object" do
|
47
|
-
|
66
|
+
load_result_object.name += " Hello World"
|
48
67
|
|
49
|
-
expect(mapper.load(input_hash)).to eq
|
68
|
+
expect(mapper.load(input_hash)).to eq load_result_object
|
50
69
|
end
|
51
70
|
|
52
|
-
scenario "dumps an result object as
|
53
|
-
expect(mapper.dump(
|
71
|
+
scenario "dumps an result object as result object" do
|
72
|
+
expect(mapper.dump(load_result_object)).to eq dump_result_object
|
54
73
|
end
|
55
74
|
|
56
75
|
end
|
@@ -86,20 +86,24 @@ describe Yaoc::ObjectMapper do
|
|
86
86
|
|
87
87
|
converter.stub(call: data)
|
88
88
|
|
89
|
-
expect(subject.
|
89
|
+
expect(subject.load_result_source).to receive(:call).with(data)
|
90
90
|
|
91
91
|
subject.load(data)
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
95
|
describe "#dump" do
|
96
|
-
it "dump the object as a hash" do
|
97
|
-
obj = subject.result_class.new 1, "paul"
|
98
96
|
|
99
|
-
|
97
|
+
it "dump the object as an wanted object" do
|
98
|
+
data = {id: 1}
|
99
|
+
|
100
|
+
reverse_converter.stub(call: data)
|
100
101
|
|
101
|
-
subject.
|
102
|
+
expect(subject.dump_result_source).to receive(:call).with(data)
|
103
|
+
|
104
|
+
subject.dump(data)
|
102
105
|
end
|
106
|
+
|
103
107
|
end
|
104
108
|
|
105
109
|
end
|
data/yaoc.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["dieter.spaeth@gmx.de"]
|
11
11
|
spec.summary = %q{Yet another object converter}
|
12
12
|
spec.description = %q{Yet another object converter}
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/slowjack2k/yaoc"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dieter Späth
|
@@ -233,7 +233,7 @@ files:
|
|
233
233
|
- spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb
|
234
234
|
- spec/unit/lib/yaoc/strategies/to_hash_mapping_spec.rb
|
235
235
|
- yaoc.gemspec
|
236
|
-
homepage:
|
236
|
+
homepage: https://github.com/slowjack2k/yaoc
|
237
237
|
licenses:
|
238
238
|
- MIT
|
239
239
|
metadata: {}
|