transproc 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/lib/transproc/array/combine.rb +4 -1
- data/lib/transproc/version.rb +1 -1
- data/spec/unit/array/combine_spec.rb +72 -64
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64dbe08d203b9ac87e38de4e8f46dde2899d5cfc
|
4
|
+
data.tar.gz: 6c4e2cd21783a8fea8e3f4ebfe05656ca8e1cd73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e46205bc38e87eb30fa40d1466bfb0f1cff727a945d985f22bb17ae9ac2c3be4910363403c08f2092514f15c2de6d06b8d5863ec9db530fa600465ea9782c601
|
7
|
+
data.tar.gz: 964e3d01aa66e74542a79ea9844977c299cab95984959d066336b3203b67364e4728938e5412a9cde5acbc7661d5f5d744e3d42d17dc89574856f4d56e8d1aff
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
# v1.0.2 2017-02-25
|
2
|
+
|
3
|
+
## Fixed
|
4
|
+
|
5
|
+
* `combine` works fine when there are missing nodes for deeply nested arrays (Kukunin)
|
6
|
+
|
7
|
+
[Compare v1.0.1...v1.0.2](https://github.com/solnic/transproc/compare/v1.0.1...v1.0.2)
|
8
|
+
|
1
9
|
# v1.0.1 2017-02-25
|
2
10
|
|
3
11
|
* `combine` is now multiple times faster, depending on the level of nesting (Kukunin + splattael)
|
@@ -1,9 +1,12 @@
|
|
1
1
|
module Transproc
|
2
2
|
module ArrayTransformations
|
3
3
|
class Combine
|
4
|
+
EMPTY_ARRAY = [].freeze
|
5
|
+
|
4
6
|
class << self
|
5
7
|
def combine(array, mappings)
|
6
8
|
root, nodes = array
|
9
|
+
return EMPTY_ARRAY if root.nil?
|
7
10
|
return root if nodes.nil?
|
8
11
|
groups = group_nodes(nodes, mappings)
|
9
12
|
|
@@ -23,7 +26,7 @@ module Transproc
|
|
23
26
|
end
|
24
27
|
|
25
28
|
def element_candidates(element, candidates, keys)
|
26
|
-
candidates[element_candidates_key(element, keys)] ||
|
29
|
+
candidates[element_candidates_key(element, keys)] || EMPTY_ARRAY
|
27
30
|
end
|
28
31
|
|
29
32
|
def group_nodes(nodes, mappings)
|
data/lib/transproc/version.rb
CHANGED
@@ -24,21 +24,6 @@ describe Transproc::ArrayTransformations do
|
|
24
24
|
it { is_expected.to eq input.first }
|
25
25
|
end
|
26
26
|
|
27
|
-
it 'does not modify its input' do
|
28
|
-
input = [
|
29
|
-
[{ name: 'Jane' }],
|
30
|
-
[[{ user: 'Jane', title: 'One' }]]
|
31
|
-
]
|
32
|
-
|
33
|
-
output = [
|
34
|
-
{ name: 'Jane', tasks: [{ user: 'Jane', title: 'One' }] }
|
35
|
-
]
|
36
|
-
|
37
|
-
combined = described_class.combine(input, [[:tasks, name: :user]])
|
38
|
-
|
39
|
-
expect(combined).to eql(output)
|
40
|
-
end
|
41
|
-
|
42
27
|
context 'with one group' do
|
43
28
|
let(:input) do
|
44
29
|
[
|
@@ -113,6 +98,78 @@ describe Transproc::ArrayTransformations do
|
|
113
98
|
end
|
114
99
|
end
|
115
100
|
|
101
|
+
context 'with non-array argument' do
|
102
|
+
let(:input) do
|
103
|
+
123
|
104
|
+
end
|
105
|
+
|
106
|
+
let(:mappings) { [[:page, {page_id: :id}]] }
|
107
|
+
|
108
|
+
it { is_expected.to eq(123) }
|
109
|
+
end
|
110
|
+
|
111
|
+
context 'with empty nested array' do
|
112
|
+
let(:input) do
|
113
|
+
[
|
114
|
+
[],
|
115
|
+
[
|
116
|
+
[]
|
117
|
+
]
|
118
|
+
]
|
119
|
+
end
|
120
|
+
|
121
|
+
let(:mappings) { [[:menu_items, {id: :menu_id}, [[:page, {page_id: :id}]]]] }
|
122
|
+
|
123
|
+
it 'does not crash' do
|
124
|
+
expect { result }.not_to raise_error
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context 'with enumerable input' do
|
129
|
+
let(:my_enumerator) do
|
130
|
+
Class.new do
|
131
|
+
include Enumerable
|
132
|
+
extend Forwardable
|
133
|
+
|
134
|
+
def_delegator :@array, :each
|
135
|
+
|
136
|
+
def initialize(array)
|
137
|
+
@array = array
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
let(:input) do
|
143
|
+
[
|
144
|
+
my_enumerator.new([
|
145
|
+
{name: 'Jane', email: 'jane@doe.org'}.freeze,
|
146
|
+
{name: 'Joe', email: 'joe@doe.org'}.freeze
|
147
|
+
].freeze),
|
148
|
+
my_enumerator.new([
|
149
|
+
my_enumerator.new([
|
150
|
+
{user: 'Jane', title: 'One'}.freeze,
|
151
|
+
{user: 'Jane', title: 'Two'}.freeze,
|
152
|
+
{user: 'Joe', title: 'Three'}.freeze
|
153
|
+
].freeze)
|
154
|
+
].freeze)
|
155
|
+
].freeze
|
156
|
+
end
|
157
|
+
let(:mappings) { [[:tasks, {name: :user}]] }
|
158
|
+
|
159
|
+
it 'supports enumerables as well' do
|
160
|
+
output = [
|
161
|
+
{name: 'Jane', email: 'jane@doe.org', tasks: [
|
162
|
+
{user: 'Jane', title: 'One'},
|
163
|
+
{user: 'Jane', title: 'Two'}
|
164
|
+
]},
|
165
|
+
{name: 'Joe', email: 'joe@doe.org', tasks: [
|
166
|
+
{user: 'Joe', title: 'Three'}
|
167
|
+
]}
|
168
|
+
]
|
169
|
+
is_expected.to eql(output)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
116
173
|
describe 'integration test' do
|
117
174
|
let(:input) do
|
118
175
|
[
|
@@ -161,54 +218,5 @@ describe Transproc::ArrayTransformations do
|
|
161
218
|
is_expected.to eql(output)
|
162
219
|
end
|
163
220
|
end
|
164
|
-
|
165
|
-
it 'merges hashes from arrays using provided join keys' do
|
166
|
-
input = [
|
167
|
-
# parent users
|
168
|
-
[
|
169
|
-
{ name: 'Jane', email: 'jane@doe.org' }.freeze,
|
170
|
-
{ name: 'Joe', email: 'joe@doe.org' }.freeze
|
171
|
-
].freeze,
|
172
|
-
[
|
173
|
-
[
|
174
|
-
# user tasks
|
175
|
-
[
|
176
|
-
{ user: 'Jane', title: 'One' }.freeze,
|
177
|
-
{ user: 'Jane', title: 'Two' }.freeze,
|
178
|
-
{ user: 'Joe', title: 'Three' }.freeze
|
179
|
-
].freeze,
|
180
|
-
[
|
181
|
-
# task tags
|
182
|
-
[
|
183
|
-
{ task: 'One', tag: 'red' }.freeze,
|
184
|
-
{ task: 'Three', tag: 'blue' }.freeze
|
185
|
-
].freeze
|
186
|
-
].freeze
|
187
|
-
].freeze
|
188
|
-
].freeze
|
189
|
-
].freeze
|
190
|
-
|
191
|
-
output = [
|
192
|
-
{ name: 'Jane', email: 'jane@doe.org', tasks: [
|
193
|
-
{ user: 'Jane', title: 'One', tags: [{ task: 'One', tag: 'red' }] },
|
194
|
-
{ user: 'Jane', title: 'Two', tags: [] }]
|
195
|
-
},
|
196
|
-
{
|
197
|
-
name: 'Joe', email: 'joe@doe.org', tasks: [
|
198
|
-
{
|
199
|
-
user: 'Joe', title: 'Three', tags: [
|
200
|
-
{ task: 'Three', tag: 'blue' }
|
201
|
-
]
|
202
|
-
}
|
203
|
-
]
|
204
|
-
}
|
205
|
-
]
|
206
|
-
|
207
|
-
combine = described_class.t(:combine, [
|
208
|
-
[:tasks, { name: :user }, [[:tags, title: :task]]]
|
209
|
-
])
|
210
|
-
|
211
|
-
expect(combine[input]).to eql(output)
|
212
|
-
end
|
213
221
|
end
|
214
222
|
end
|