two-way-mapper 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +15 -1
- data/lib/two_way_mapper/mapping.rb +22 -13
- data/lib/two_way_mapper/version.rb +1 -1
- data/spec/mapping_spec.rb +32 -6
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27c5a5f04384aebefba51e9ec085042cdf6d1ca40ff7d82aafba522b3b8eb457
|
4
|
+
data.tar.gz: 52d3d15700603d35a82e12f53c64765c71053644238bbe02cb44e9666934b6f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d1319a99792280e03ea43e6b8ecb294cb6da12d443e4b7f4a1211d4ef44515589de48307244330044baa666c4691f4816b7466ac56401ced959321afb263685
|
7
|
+
data.tar.gz: 4e99bb150c889eb4eafcb966ba7f3756cee4d64e63f2ec0b0040f19f2d830e75d1f433234018ca11a7506bb7b35a5962a74cf491b5a0166b3783bf60793f25b1
|
data/README.md
CHANGED
@@ -38,6 +38,20 @@ TwoWayMapper.register :customer do |mapping|
|
|
38
38
|
end
|
39
39
|
```
|
40
40
|
|
41
|
+
For simple rules use
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
TwoWayMapper.register :customer do |mapping|
|
45
|
+
mapping.left :object # set left plugin to object
|
46
|
+
mapping.right :hash, stringify_keys: true # set right plugin to hash
|
47
|
+
|
48
|
+
mapping.rules(
|
49
|
+
'first_name' => 'FirstName',
|
50
|
+
'last_name' => 'LastName'
|
51
|
+
)
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
41
55
|
Mapping can be defined explicitly without registration
|
42
56
|
|
43
57
|
```ruby
|
@@ -47,7 +61,7 @@ mapping.right :hash, stringify_keys: true
|
|
47
61
|
# ...
|
48
62
|
```
|
49
63
|
|
50
|
-
Once mapping is defined
|
64
|
+
Once the mapping is defined it can be used to convert one object to another and vice versa
|
51
65
|
|
52
66
|
```ruby
|
53
67
|
Customer = Struct.new :first_name, :last_name, :gender
|
@@ -4,10 +4,10 @@ module TwoWayMapper
|
|
4
4
|
class Mapping
|
5
5
|
DIRECTIONS = [:left, :right].freeze
|
6
6
|
|
7
|
-
attr_reader :
|
7
|
+
attr_reader :rules_list, :left_class, :left_options, :right_class, :right_options
|
8
8
|
|
9
9
|
def initialize
|
10
|
-
@
|
10
|
+
@rules_list = []
|
11
11
|
end
|
12
12
|
|
13
13
|
[DIRECTIONS, DIRECTIONS.reverse].each do |from, to|
|
@@ -18,7 +18,7 @@ module TwoWayMapper
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def #{from}_selectors(mappable: false)
|
21
|
-
|
21
|
+
rules_list.flat_map do |rule|
|
22
22
|
if mappable && rule.from_#{from}_to_#{to}_only?
|
23
23
|
[]
|
24
24
|
else
|
@@ -28,15 +28,15 @@ module TwoWayMapper
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def from_#{from}_to_#{to}(left_obj, right_obj)
|
31
|
-
|
31
|
+
rules_list.each { |rule| rule.from_#{from}_to_#{to}(left_obj, right_obj) }
|
32
32
|
#{to}_obj
|
33
33
|
end
|
34
34
|
CODE
|
35
35
|
end
|
36
36
|
|
37
37
|
def rule(left_selectors, right_selectors = {}, options = {})
|
38
|
-
raise '
|
39
|
-
raise '
|
38
|
+
raise 'left is not defined' unless left_class
|
39
|
+
raise 'right is not defined' unless right_class
|
40
40
|
|
41
41
|
opt = options.dup
|
42
42
|
left_opt = opt.delete(:left) || {}
|
@@ -52,14 +52,17 @@ module TwoWayMapper
|
|
52
52
|
right_opt.merge!(opt.delete(right_selectors))
|
53
53
|
end
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
55
|
+
@rules_list << Rule.new(
|
56
|
+
build_nodes(left_selectors, left_class, left_options.merge(left_opt)),
|
57
|
+
build_nodes(right_selectors, right_class, right_options.merge(right_opt)),
|
58
|
+
opt
|
59
|
+
)
|
60
|
+
end
|
61
61
|
|
62
|
-
|
62
|
+
def rules(hash)
|
63
|
+
hash.each do |left_selector, right_selector|
|
64
|
+
rule(left_selector, right_selector)
|
65
|
+
end
|
63
66
|
end
|
64
67
|
|
65
68
|
private
|
@@ -69,5 +72,11 @@ module TwoWayMapper
|
|
69
72
|
rescue NameError
|
70
73
|
raise NameError, 'Cannot find node'
|
71
74
|
end
|
75
|
+
|
76
|
+
def build_nodes(selectors, klass, opt)
|
77
|
+
Array(selectors).map do |selector|
|
78
|
+
klass.new(selector, opt)
|
79
|
+
end
|
80
|
+
end
|
72
81
|
end
|
73
82
|
end
|
data/spec/mapping_spec.rb
CHANGED
@@ -83,9 +83,9 @@ describe TwoWayMapper::Mapping do
|
|
83
83
|
end
|
84
84
|
|
85
85
|
it 'should add item to rules hash' do
|
86
|
-
expect { mapping.rule 'key1', 'Key1' }.to change { mapping.
|
86
|
+
expect { mapping.rule 'key1', 'Key1' }.to change { mapping.rules_list.count }.from(0).to(1)
|
87
87
|
|
88
|
-
rule = mapping.
|
88
|
+
rule = mapping.rules_list.first
|
89
89
|
expect(rule).to be_instance_of TwoWayMapper::Rule
|
90
90
|
expect(rule.left_nodes.map(&:selector)).to eql ['key1']
|
91
91
|
expect(rule.right_nodes.map(&:selector)).to eql ['Key1']
|
@@ -94,7 +94,7 @@ describe TwoWayMapper::Mapping do
|
|
94
94
|
it 'should support multiple selectors' do
|
95
95
|
mapping.rule ['key1', 'key2'], ['Key1', 'Key2']
|
96
96
|
|
97
|
-
rule = mapping.
|
97
|
+
rule = mapping.rules_list.first
|
98
98
|
|
99
99
|
expect(rule.left_nodes.size).to eql 2
|
100
100
|
expect(rule.left_nodes[0].selector).to eql 'key1'
|
@@ -108,7 +108,7 @@ describe TwoWayMapper::Mapping do
|
|
108
108
|
expect { mapping.rule 'key1' => { opt1: 'val' } }.to raise_error StandardError
|
109
109
|
|
110
110
|
mapping.rule 'key1' => { opt1: 'val' }, 'Key2' => {}
|
111
|
-
rule = mapping.
|
111
|
+
rule = mapping.rules_list.first
|
112
112
|
|
113
113
|
expect(rule.left_nodes.map(&:selector)).to eql ['key1']
|
114
114
|
expect(rule.right_nodes.map(&:selector)).to eql ['Key2']
|
@@ -116,7 +116,7 @@ describe TwoWayMapper::Mapping do
|
|
116
116
|
|
117
117
|
it 'should allow to pass left abd right options ' do
|
118
118
|
mapping.rule 'key1', 'Key2', left: { opt1: 'val' }, right: { opt2: 'val' }
|
119
|
-
rule = mapping.
|
119
|
+
rule = mapping.rules_list.first
|
120
120
|
|
121
121
|
expect(rule.left_nodes.first.options).to include opt1: 'val'
|
122
122
|
expect(rule.right_nodes.first.options).to include opt2: 'val'
|
@@ -139,7 +139,7 @@ describe TwoWayMapper::Mapping do
|
|
139
139
|
before :each do
|
140
140
|
mapping.left :object
|
141
141
|
mapping.right :hash
|
142
|
-
allow(mapping).to receive(:
|
142
|
+
allow(mapping).to receive(:rules_list).and_return [rule1, rule2]
|
143
143
|
end
|
144
144
|
|
145
145
|
[described_class::DIRECTIONS, described_class::DIRECTIONS.reverse].each do |from, to|
|
@@ -155,4 +155,30 @@ describe TwoWayMapper::Mapping do
|
|
155
155
|
end
|
156
156
|
end
|
157
157
|
end
|
158
|
+
|
159
|
+
describe '#rules' do
|
160
|
+
let(:hash) do
|
161
|
+
{
|
162
|
+
key1: :Key1,
|
163
|
+
key2: :Key2
|
164
|
+
}
|
165
|
+
end
|
166
|
+
|
167
|
+
before :each do
|
168
|
+
mapping.left :object
|
169
|
+
mapping.right :hash
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'should allow to define multiple rules' do
|
173
|
+
expect { mapping.rules(hash) }.to change { mapping.rules_list.count }.from(0).to(2)
|
174
|
+
|
175
|
+
hash.each_with_index do |(left, right), index|
|
176
|
+
rule = mapping.rules_list[index]
|
177
|
+
|
178
|
+
expect(rule).to be_instance_of TwoWayMapper::Rule
|
179
|
+
expect(rule.left_nodes.map(&:selector)).to eql [left]
|
180
|
+
expect(rule.right_nodes.map(&:selector)).to eql [right]
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
158
184
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: two-way-mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tim Masliuchenko
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-07-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -123,7 +123,7 @@ homepage: https://github.com/timsly/two-way-mapper
|
|
123
123
|
licenses:
|
124
124
|
- MIT
|
125
125
|
metadata: {}
|
126
|
-
post_install_message:
|
126
|
+
post_install_message:
|
127
127
|
rdoc_options: []
|
128
128
|
require_paths:
|
129
129
|
- lib
|
@@ -139,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
139
139
|
version: '0'
|
140
140
|
requirements: []
|
141
141
|
rubygems_version: 3.0.3
|
142
|
-
signing_key:
|
142
|
+
signing_key:
|
143
143
|
specification_version: 4
|
144
144
|
summary: Two way data mapping
|
145
145
|
test_files:
|