transproc 0.4.1 → 0.4.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 +12 -0
- data/lib/transproc/array.rb +1 -1
- data/lib/transproc/hash.rb +22 -5
- data/lib/transproc/transformer.rb +1 -1
- data/lib/transproc/version.rb +1 -1
- data/spec/unit/array_transformations_spec.rb +8 -0
- data/spec/unit/hash_transformations_spec.rb +22 -0
- 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: 5fb2ca80c956f246b5277462bb1dcbf5c3dbd09d
|
4
|
+
data.tar.gz: 7ae7b71a375e026ca281fbb6906850e418ba2b21
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5fcb3e8a8229938d4f2cf16dab007eea0d3b7fbcb09c901c3a7aec87d5ceaa99ebd8e7254400409b4d540fb6426d5ba9d0b6182d36450d27a480430473c68c7
|
7
|
+
data.tar.gz: 708adcb9250af48b08950fdd85e82b6168d468c02b089544b97db283854be4852c37c347c4606779cee336bbf10ed6cc18bc4d595fea44db2fe5be7fd3dbb4b1
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
## v0.4.2 2017-01-12
|
2
|
+
|
3
|
+
## Added
|
4
|
+
|
5
|
+
* prefix option to HashTransformations#unwrap and HashTransformations#unwrap! (AMHOL)
|
6
|
+
|
7
|
+
## Fixed
|
8
|
+
|
9
|
+
* `map_array` won't cause a SystemStackError in case of gigantic arrays (solnic)
|
10
|
+
|
11
|
+
[Compare v0.4.1...v0.4.2](https://github.com/solnic/transproc/compare/v0.4.1...v0.4.2)
|
12
|
+
|
1
13
|
## v0.4.1 2016-11-08
|
2
14
|
|
3
15
|
## Added
|
data/lib/transproc/array.rb
CHANGED
data/lib/transproc/hash.rb
CHANGED
@@ -318,14 +318,19 @@ module Transproc
|
|
318
318
|
# Transproc(:unwrap, :address, [:street, :zipcode])[address: { street: 'Street', zipcode: '123' }]
|
319
319
|
# # => {street: "Street", zipcode: "123"}
|
320
320
|
#
|
321
|
-
# @param [Hash]
|
321
|
+
# @param [Hash] hash
|
322
|
+
# @param [Mixed] root The root key to unwrap values from
|
323
|
+
# @param [Array] keys The keys that should be unwrapped (optional)
|
324
|
+
# @param [Hash] options hash of options (optional)
|
325
|
+
# @option options [Boolean] :prefix if true, unwrapped keys will be prefixed
|
326
|
+
# with the root key followed by an underscore (_)
|
322
327
|
#
|
323
328
|
# @return [Hash]
|
324
329
|
#
|
325
330
|
# @api public
|
326
|
-
def self.unwrap(hash, root, keys = nil)
|
331
|
+
def self.unwrap(hash, root, keys = nil, prefix: false)
|
327
332
|
copy = Hash[hash].merge(root => Hash[hash[root]])
|
328
|
-
unwrap!(copy, root, keys)
|
333
|
+
unwrap!(copy, root, keys, prefix: prefix)
|
329
334
|
end
|
330
335
|
|
331
336
|
# Same as `:unwrap` but mutates the hash
|
@@ -333,11 +338,23 @@ module Transproc
|
|
333
338
|
# @see HashTransformations.unwrap
|
334
339
|
#
|
335
340
|
# @api public
|
336
|
-
def self.unwrap!(hash, root, selected = nil)
|
341
|
+
def self.unwrap!(hash, root, selected = nil, prefix: false)
|
337
342
|
if nested_hash = hash[root]
|
338
343
|
keys = nested_hash.keys
|
339
344
|
keys &= selected if selected
|
340
|
-
|
345
|
+
new_keys = if prefix
|
346
|
+
keys.map do |key|
|
347
|
+
if root.is_a?(::Symbol)
|
348
|
+
[root, key].join('_').to_sym
|
349
|
+
else
|
350
|
+
[root, key].join('_')
|
351
|
+
end
|
352
|
+
end
|
353
|
+
else
|
354
|
+
keys
|
355
|
+
end
|
356
|
+
|
357
|
+
hash.update(Hash[new_keys.zip(keys.map { |key| nested_hash.delete(key) })])
|
341
358
|
hash.delete(root) if nested_hash.empty?
|
342
359
|
end
|
343
360
|
|
data/lib/transproc/version.rb
CHANGED
@@ -132,6 +132,14 @@ describe Transproc::ArrayTransformations do
|
|
132
132
|
expect(map[input]).to eql(output)
|
133
133
|
expect(input).to eql(original)
|
134
134
|
end
|
135
|
+
|
136
|
+
it 'handles huge arrays' do
|
137
|
+
map = described_class.t(:map_array, hashes[:symbolize_keys])
|
138
|
+
|
139
|
+
input = 138706.times.map { |i| { 'key' => i } }
|
140
|
+
|
141
|
+
expect { map[input] }.to_not raise_error(SystemStackError, /stack level too deep/)
|
142
|
+
end
|
135
143
|
end
|
136
144
|
|
137
145
|
describe '.map_array!' do
|
@@ -318,6 +318,28 @@ describe Transproc::HashTransformations do
|
|
318
318
|
|
319
319
|
expect(input).to eql(output)
|
320
320
|
end
|
321
|
+
|
322
|
+
it 'prefixes unwrapped keys and retains root string type if prefix option is truthy' do
|
323
|
+
unwrap = described_class.t(:unwrap!, 'wrapped', prefix: true)
|
324
|
+
|
325
|
+
input = { 'wrapped' => { one: nil, two: false } }
|
326
|
+
output = { 'wrapped_one' => nil, 'wrapped_two' => false }
|
327
|
+
|
328
|
+
unwrap[input]
|
329
|
+
|
330
|
+
expect(input).to eql(output)
|
331
|
+
end
|
332
|
+
|
333
|
+
it 'prefixes unwrapped keys and retains root type if prefix option is truthy' do
|
334
|
+
unwrap = described_class.t(:unwrap!, :wrapped, prefix: true)
|
335
|
+
|
336
|
+
input = { wrapped: { 'one' => nil, 'two' => false } }
|
337
|
+
output = { wrapped_one: nil, wrapped_two: false }
|
338
|
+
|
339
|
+
unwrap[input]
|
340
|
+
|
341
|
+
expect(input).to eql(output)
|
342
|
+
end
|
321
343
|
end
|
322
344
|
|
323
345
|
describe '.unwrap' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: transproc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|