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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 76ec2952fb54e5e56fe3b9026a5a25526555f7dc
4
- data.tar.gz: e6c3346aa655eb3efeb2544e34540d835cf3a5fe
3
+ metadata.gz: 5fb2ca80c956f246b5277462bb1dcbf5c3dbd09d
4
+ data.tar.gz: 7ae7b71a375e026ca281fbb6906850e418ba2b21
5
5
  SHA512:
6
- metadata.gz: 664e5c3008cd510d42708d08514e3482b5fa018c9cb5dfb9ad9f310aba1b251380147f03cba0ceab2f3f6b5b5ea42bab4c69324d758ec1298060c0c2131bd641
7
- data.tar.gz: ef2d05a99e725737443d18292a31cd9081eab26f0c95a90b257b223059b26f906f7ac577fd6a318540d08a4fa1afabe83bdc9618443872e38f02606fc86f7008
6
+ metadata.gz: d5fcb3e8a8229938d4f2cf16dab007eea0d3b7fbcb09c901c3a7aec87d5ceaa99ebd8e7254400409b4d540fb6426d5ba9d0b6182d36450d27a480430473c68c7
7
+ data.tar.gz: 708adcb9250af48b08950fdd85e82b6168d468c02b089544b97db283854be4852c37c347c4606779cee336bbf10ed6cc18bc4d595fea44db2fe5be7fd3dbb4b1
@@ -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
@@ -38,7 +38,7 @@ module Transproc
38
38
  #
39
39
  # @api public
40
40
  def self.map_array(array, fn)
41
- map_array!(Array[*array], fn)
41
+ map_array!(array.dup, fn)
42
42
  end
43
43
 
44
44
  # Same as `map_array` but mutates the array
@@ -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
- hash.update(Hash[keys.zip(keys.map { |key| nested_hash.delete(key) })])
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
 
@@ -15,7 +15,7 @@ module Transproc
15
15
  # include Anima.new(:city, :street, :zipcode)
16
16
  # end
17
17
  #
18
- # class UsersMapper
18
+ # class UsersMapper < Transproc::Transformer
19
19
  # map_array do
20
20
  # symbolize_keys
21
21
  # rename_keys user_name: :name
@@ -1,3 +1,3 @@
1
1
  module Transproc
2
- VERSION = '0.4.1'.freeze
2
+ VERSION = '0.4.2'.freeze
3
3
  end
@@ -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.1
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: 2016-11-08 00:00:00.000000000 Z
11
+ date: 2017-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler