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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd81b477e8c02b0e1644d7dd91c0dd798a09d24f
4
- data.tar.gz: 6dde0346785ecc1de03f0beb9458cee9c427b5e1
3
+ metadata.gz: 64dbe08d203b9ac87e38de4e8f46dde2899d5cfc
4
+ data.tar.gz: 6c4e2cd21783a8fea8e3f4ebfe05656ca8e1cd73
5
5
  SHA512:
6
- metadata.gz: da55a09ce5a269141913ef066754d2e49a6e0cd45debdac119df3aff3788d0a287e654b43590c9c73223fd2b8ccfed48e4a6c850889259747414517c4eef21ad
7
- data.tar.gz: 37c85578845d113c1f792cc02d83c9b697c585c8426fe8f76dc3b913a7ff307b97bc226cf039a95f40a1861d091e9a339b13016f71a512a7557e0a2428592a11
6
+ metadata.gz: e46205bc38e87eb30fa40d1466bfb0f1cff727a945d985f22bb17ae9ac2c3be4910363403c08f2092514f15c2de6d06b8d5863ec9db530fa600465ea9782c601
7
+ data.tar.gz: 964e3d01aa66e74542a79ea9844977c299cab95984959d066336b3203b67364e4728938e5412a9cde5acbc7661d5f5d744e3d42d17dc89574856f4d56e8d1aff
@@ -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)
@@ -1,3 +1,3 @@
1
1
  module Transproc
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.0.2'.freeze
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transproc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica