rekey 3.0.1 → 4.0.0

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: 6a5afeefc751d6d94e863fbc3a0728323f9c8379
4
- data.tar.gz: c79b4c27daf11a1905fe2e5d09d0b07be8cc5f5f
3
+ metadata.gz: 9fd354b9d3c1d9586254cada81be1e6bb9727a4a
4
+ data.tar.gz: 35a6a2235d235543d2ce42cd483f98ba8ea3da38
5
5
  SHA512:
6
- metadata.gz: 98b0ac1857651dd1d2f561cf10fbb466ab3bedd10c1068702454c0aaa4750c1f5edea75c7096d164a5316e335f0e7805aa2fa145c7ce63cdb48d8bcde0f71624
7
- data.tar.gz: 76a0051babba822500df84772fec87e7686fa29349d14e98239162f81e34202ce774b3a45cf07cffa92206d5c8f6747aa46a56781d8b5582ad0dc83e20605517
6
+ metadata.gz: f3dba9ce015ac3bd8cb5c6b0e9c1f887c2127453649e2d0357e52d710ddc67970a8ae2747d3b1686198ed29533b66c3445df3ef227d69b39c6666c075b51301c
7
+ data.tar.gz: b9b139e019b4587f06dd0c1a1fed7e7fb9e7bd78c72af91cdb2202a8015612c1f268e10b121cfcb23e12ac8160e2b10f83368b55cf6d6e400cb8048bbc81e62e
@@ -2,7 +2,7 @@ require_relative 'rekey/rekey'
2
2
 
3
3
 
4
4
  module Rekey
5
- VERSION = '3.0.1'
5
+ VERSION = '4.0.0'
6
6
  end
7
7
 
8
8
 
@@ -2,77 +2,61 @@ require 'pluckit/pluck'
2
2
 
3
3
 
4
4
  module Rekey
5
- class << self
5
+ extend self
6
6
 
7
- def rekey(enumerable, key_handle = nil, value_handle = nil, &block)
8
- validate_input key_handle, value_handle, &block
7
+ def rekey(enumerable, key_handle = nil, value_handle = nil, &block)
8
+ validate_input key_handle, value_handle, &block
9
9
 
10
- key_value_fn = if enumerable.respond_to?(:keys)
11
- proc {|k, v| [k, v]}
12
- else
13
- proc {|v| [nil, v]}
14
- end
15
-
16
- # determine return type
17
- res = if key_handle or block or enumerable.respond_to?(:keys)
18
- {}
19
- else
20
- []
21
- end
10
+ key_value_fn = if enumerable.respond_to?(:keys)
11
+ proc {|k, v| [k, v]}
12
+ else
13
+ proc {|v| [nil, v]}
14
+ end
22
15
 
23
- # rekey input
24
- enumerable.each_with_object(res) do |*args, res|
25
- key, value = key_value_fn.call *args
26
- new_key = key
27
- new_value = value
16
+ # rekey input
17
+ enumerable.each_with_object({}) do |*args, res|
18
+ key, value = key_value_fn.call *args
19
+ new_key = key
20
+ new_value = value
28
21
 
29
- if block
30
- if block.arity <= 0
31
- # function pointer, eg. &:to_i
32
- new_key = block.call value
33
- elsif block.arity == 1
34
- # standard block
35
- new_key = block.call value
36
- else
37
- # block that wants both key and value
38
- new_key = block.call key, value
39
- end
22
+ if block
23
+ if block.arity < 2
24
+ # block only wants value
25
+ # arity -1 is a function pointer, eg. &:to_i
26
+ new_key = block.call value
40
27
  else
41
- new_key = PluckIt.pluckit value, key_handle if key_handle
42
- new_value = PluckIt.pluckit value, value_handle if value_handle
43
- end
28
+ # block wants both key and value
44
29
 
45
- # collect results
46
- if res.is_a? Array
47
- unless new_key.nil?
48
- # safeguard against stupidity
30
+ if key_value_fn.arity != 2
49
31
  raise ArgumentError.new(
50
- "not expecting a key value, got: #{new_key}"
32
+ 'rekey block requests key/value pair but only value available'
51
33
  )
52
34
  end
53
35
 
54
- res.push new_value
55
- else
56
- res[new_key] = new_value
36
+ new_key = block.call key, value
57
37
  end
38
+ else
39
+ new_key = PluckIt.pluckit value, key_handle
40
+ new_value = PluckIt.pluckit value, value_handle if value_handle
58
41
  end
42
+
43
+ # collect results
44
+ res[new_key] = new_value
59
45
  end
46
+ end
60
47
 
61
48
 
62
- private
49
+ private
63
50
 
64
- def validate_input key_handle, value_handle, &block
65
- if block
66
- if (key_handle or value_handle)
67
- raise ArgumentError.new 'expected key / value handles, *or* block'
68
- end
69
- else
70
- unless key_handle or value_handle
71
- raise ArgumentError.new 'key_handle and/or value_handle are required'
72
- end
51
+ def validate_input key_handle, value_handle, &block
52
+ if block
53
+ if (key_handle or value_handle)
54
+ raise ArgumentError.new 'expected key / value handles, *or* block'
73
55
  end
56
+ elsif key_handle.nil?
57
+ raise ArgumentError.new 'key handle or block required'
74
58
  end
59
+ end
75
60
 
76
61
 
77
- end
78
62
  end
@@ -1,5 +1,5 @@
1
1
  require 'minitest/autorun'
2
- require 'rekey'
2
+ require_relative '../lib/rekey'
3
3
 
4
4
 
5
5
  class RekeyArrayBlockTest < Minitest::Test
@@ -8,7 +8,7 @@ class RekeyArrayBlockTest < Minitest::Test
8
8
  @input = [*1..3]
9
9
  end
10
10
 
11
- def test_keys
11
+ def test_all
12
12
  assert_equal({
13
13
  1 => 1,
14
14
  2 => 2,
@@ -17,6 +17,14 @@ class RekeyArrayBlockTest < Minitest::Test
17
17
  @input.rekey {|v| v }
18
18
  )
19
19
 
20
+ assert_equal({
21
+ 1 => 1,
22
+ 2 => 2,
23
+ 3 => 3,
24
+ },
25
+ @input.rekey(&:itself)
26
+ )
27
+
20
28
  assert_equal({
21
29
  2 => 1,
22
30
  4 => 2,
@@ -32,12 +40,15 @@ class RekeyArrayBlockTest < Minitest::Test
32
40
  },
33
41
  @input.rekey {|v| v.to_s }
34
42
  )
35
- end
36
43
 
37
- def test_nil_key
38
44
  assert_equal(
39
45
  { nil => 3 },
40
- @input.rekey {|k, v| nil }
46
+ @input.rekey {|v| nil }
47
+ )
48
+
49
+ assert_equal(
50
+ { 123 => 3 },
51
+ @input.rekey { 123 }
41
52
  )
42
53
  end
43
54
 
@@ -1,5 +1,5 @@
1
1
  require 'minitest/autorun'
2
- require 'rekey'
2
+ require_relative '../lib/rekey'
3
3
 
4
4
 
5
5
  class RekeyArrayHandlersTest < Minitest::Test
@@ -8,31 +8,6 @@ class RekeyArrayHandlersTest < Minitest::Test
8
8
  @input = [*1..3]
9
9
  end
10
10
 
11
- def test_both
12
- assert_equal({
13
- 1 => 1,
14
- 2 => 2,
15
- 3 => 3,
16
- },
17
- @input.rekey(:to_i, :to_i)
18
- )
19
-
20
- assert_equal({
21
- '1' => 1.0,
22
- '2' => 2.0,
23
- '3' => 3.0,
24
- },
25
- @input.rekey(:to_s, :to_f)
26
- )
27
-
28
- assert_equal({
29
- '1' => 1.0,
30
- '2' => 2.0,
31
- '3' => 3.0,
32
- },
33
- @input.rekey('to_s', 'to_f')
34
- )
35
- end
36
11
 
37
12
  def test_keys
38
13
  assert_equal({
@@ -68,23 +43,34 @@ class RekeyArrayHandlersTest < Minitest::Test
68
43
  )
69
44
  end
70
45
 
71
- def test_values
72
- assert_equal(
73
- @input,
74
- @input.rekey(nil, :to_i)
46
+
47
+ def test_both
48
+ assert_equal({
49
+ 1 => 1,
50
+ 2 => 2,
51
+ 3 => 3,
52
+ },
53
+ @input.rekey(:to_i, :to_i)
75
54
  )
76
55
 
77
- assert_equal(
78
- @input.map(&:to_f),
79
- @input.rekey(nil, :to_f)
56
+ assert_equal({
57
+ '1' => 1.0,
58
+ '2' => 2.0,
59
+ '3' => 3.0,
60
+ },
61
+ @input.rekey(:to_s, :to_f)
80
62
  )
81
63
 
82
- assert_equal(
83
- @input.map(&:to_s),
84
- @input.rekey(nil, 'to_s')
64
+ assert_equal({
65
+ '1' => 1.0,
66
+ '2' => 2.0,
67
+ '3' => 3.0,
68
+ },
69
+ @input.rekey('to_s', 'to_f')
85
70
  )
86
71
  end
87
72
 
73
+
88
74
  def test_array_indices
89
75
  array_data = [
90
76
  [ 1, 2, 3 ],
@@ -92,16 +78,6 @@ class RekeyArrayHandlersTest < Minitest::Test
92
78
  [ 7, 8, 9 ],
93
79
  ]
94
80
 
95
- assert_equal(
96
- array_data,
97
- array_data.rekey(nil, :to_a)
98
- )
99
-
100
- assert_equal(
101
- [ 1, 4, 7 ],
102
- array_data.rekey(nil, 0)
103
- )
104
-
105
81
  assert_equal({
106
82
  1 => 3,
107
83
  4 => 6,
@@ -1,5 +1,6 @@
1
1
  require 'minitest/autorun'
2
- require 'rekey'
2
+ require 'set'
3
+ require_relative '../lib/rekey'
3
4
 
4
5
 
5
6
  class RekeyEmptyInputTest < Minitest::Test
@@ -19,11 +20,6 @@ class RekeyEmptyInputTest < Minitest::Test
19
20
  {},
20
21
  Set.new.rekey(:k, :v)
21
22
  )
22
-
23
- assert_equal(
24
- [],
25
- Set.new.rekey(nil, :k)
26
- )
27
23
  end
28
24
 
29
25
  end
@@ -1,5 +1,5 @@
1
1
  require 'minitest/autorun'
2
- require 'rekey'
2
+ require_relative '../lib/rekey'
3
3
 
4
4
 
5
5
  class RekeyHashBlockTest < Minitest::Test
@@ -1,8 +1,9 @@
1
1
  require 'minitest/autorun'
2
- require 'rekey'
2
+ require_relative '../lib/rekey'
3
3
 
4
4
 
5
5
  class RekeyHashHandlersTest < Minitest::Test
6
+
6
7
  def setup
7
8
  @input = {
8
9
  a: 1,
@@ -11,32 +12,6 @@ class RekeyHashHandlersTest < Minitest::Test
11
12
  }
12
13
  end
13
14
 
14
- def test_both
15
- assert_equal({
16
- 1 => 1,
17
- 2 => 2,
18
- 3 => 3,
19
- },
20
- @input.rekey(:to_i, :to_i)
21
- )
22
-
23
- assert_equal({
24
- '1' => 1.0,
25
- '2' => 2.0,
26
- '3' => 3.0,
27
- },
28
- @input.rekey(:to_s, :to_i)
29
- )
30
-
31
- assert_equal({
32
- '1' => 1.0,
33
- '2' => 2.0,
34
- '3' => 3.0,
35
- },
36
- @input.rekey('to_s', 'to_i')
37
- )
38
-
39
- end
40
15
 
41
16
  def test_keys
42
17
  assert_equal({
@@ -56,29 +31,34 @@ class RekeyHashHandlersTest < Minitest::Test
56
31
  )
57
32
  end
58
33
 
59
- def test_values
60
- assert_equal(
61
- @input,
62
- @input.rekey(nil, :to_i)
34
+
35
+ def test_both
36
+ assert_equal({
37
+ 1 => 1,
38
+ 2 => 2,
39
+ 3 => 3,
40
+ },
41
+ @input.rekey(:to_i, :to_i)
63
42
  )
64
43
 
65
44
  assert_equal({
66
- a: '1',
67
- b: '2',
68
- c: '3',
45
+ '1' => 1.0,
46
+ '2' => 2.0,
47
+ '3' => 3.0,
69
48
  },
70
- @input.rekey(nil, :to_s)
49
+ @input.rekey(:to_s, :to_i)
71
50
  )
72
51
 
73
52
  assert_equal({
74
- a: '1',
75
- b: '2',
76
- c: '3',
53
+ '1' => 1.0,
54
+ '2' => 2.0,
55
+ '3' => 3.0,
77
56
  },
78
- @input.rekey(nil, 'to_s')
57
+ @input.rekey('to_s', 'to_i')
79
58
  )
80
59
  end
81
60
 
61
+
82
62
  def test_hash_array
83
63
  data = {
84
64
  a: [ 1, 2, 3 ],
@@ -86,14 +66,6 @@ class RekeyHashHandlersTest < Minitest::Test
86
66
  c: [ 7, 8, 9 ],
87
67
  }
88
68
 
89
- assert_equal({
90
- a: 1,
91
- b: 4,
92
- c: 7,
93
- },
94
- data.rekey(nil, 0)
95
- )
96
-
97
69
  assert_equal({
98
70
  1 => 2,
99
71
  4 => 5,
@@ -101,8 +73,17 @@ class RekeyHashHandlersTest < Minitest::Test
101
73
  },
102
74
  data.rekey(0, 1)
103
75
  )
76
+
77
+ assert_equal({
78
+ 1 => 3,
79
+ 4 => 6,
80
+ 7 => 9,
81
+ },
82
+ data.rekey(:min, :max)
83
+ )
104
84
  end
105
85
 
86
+
106
87
  def test_hash_hash
107
88
  data = {
108
89
  a: { i: 1, v: 2 },
@@ -111,11 +92,11 @@ class RekeyHashHandlersTest < Minitest::Test
111
92
  }
112
93
 
113
94
  assert_equal({
114
- a: 1,
115
- b: 2,
116
- c: 3,
95
+ 1 => { i: 1, v: 2 },
96
+ 2 => { i: 2, v: 4 },
97
+ 3 => { i: 3, v: 6 },
117
98
  },
118
- data.rekey(nil, :i)
99
+ data.rekey(:i)
119
100
  )
120
101
 
121
102
  assert_equal({
@@ -1,5 +1,5 @@
1
1
  require 'minitest/autorun'
2
- require 'rekey'
2
+ require_relative '../lib/rekey'
3
3
 
4
4
 
5
5
  class RekeyInvalidInputTest < Minitest::Test
@@ -8,18 +8,43 @@ class RekeyInvalidInputTest < Minitest::Test
8
8
  end
9
9
 
10
10
 
11
- def test_all
11
+ def test_nil
12
12
  assert_raises ArgumentError do
13
13
  @input.rekey
14
14
  end
15
15
 
16
16
  assert_raises ArgumentError do
17
- @input.rekey(:k) do |k| 1 end
17
+ @input.rekey nil
18
18
  end
19
19
 
20
20
  assert_raises ArgumentError do
21
- @input.rekey(:k, :v) do |k| 1 end
21
+ @input.rekey nil, :v
22
22
  end
23
+
24
+ # unfortunately, these still pass
25
+ @input.rekey(nil) {|v| v}
26
+ @input.rekey(nil, nil) {|v| v}
23
27
  end
24
28
 
29
+
30
+ def test_handles_and_block
31
+ assert_raises ArgumentError do
32
+ @input.rekey(:k) {|k| 1}
33
+ end
34
+
35
+ assert_raises ArgumentError do
36
+ @input.rekey(:k, :v) {|k| 1}
37
+ end
38
+ end
39
+
40
+
41
+ def test_block_underflow
42
+ # block requesting key and value, but @input
43
+ # doesn't have keys
44
+ assert_raises ArgumentError do
45
+ @input.rekey {|k, v| 1}
46
+ end
47
+ end
48
+
49
+
25
50
  end
@@ -1,11 +1,10 @@
1
1
  require 'minitest/autorun'
2
2
  require 'set'
3
-
4
- $LOAD_PATH.unshift 'lib'
5
- require 'rekey/rekey'
3
+ require_relative '../lib/rekey/rekey'
6
4
 
7
5
 
8
6
  class MonkeyPatchingTest < Minitest::Test
7
+
9
8
  def test_not_installed
10
9
  if defined? Rekey::VERSION
11
10
  # entire library was loaded, so these tests are invalid.
@@ -33,4 +32,13 @@ class MonkeyPatchingTest < Minitest::Test
33
32
  end
34
33
 
35
34
 
35
+ def test_pluckit
36
+ assert PluckIt
37
+
38
+ assert_raises NoMethodError do
39
+ [ 1, 2, 3 ].pluck 0
40
+ end
41
+ end
42
+
43
+
36
44
  end
@@ -1,5 +1,5 @@
1
1
  require 'minitest/autorun'
2
- require 'rekey'
2
+ require_relative '../lib/rekey'
3
3
 
4
4
 
5
5
  class RekeyRangeTest < Minitest::Test
@@ -12,25 +12,11 @@ class RekeyRangeTest < Minitest::Test
12
12
  end
13
13
 
14
14
  def test_stuff
15
- assert_equal([
16
- [ 1, 2 ],
17
- [ 4, 5 ],
18
- ],
19
- @input.rekey(nil, 0..1)
20
- )
21
-
22
- assert_equal([
23
- [ 2 ],
24
- [ 5 ],
25
- ],
26
- @input.rekey(nil, 1...2)
27
- )
28
-
29
15
  assert_equal({
30
- [ 2 ] => @input[0],
31
- [ 5 ] => @input[1],
16
+ [ 1, 2 ] => [ 1, 2, 3 ],
17
+ [ 4, 5 ] => [ 4, 5, 6 ],
32
18
  },
33
- @input.rekey(1...2)
19
+ @input.rekey(0..1)
34
20
  )
35
21
 
36
22
  assert_equal({
@@ -1,29 +1,29 @@
1
1
  require 'minitest/autorun'
2
- require 'rekey'
2
+ require_relative '../lib/rekey'
3
3
 
4
4
 
5
5
  class RekeyRegexpTest < Minitest::Test
6
6
 
7
7
  def setup
8
8
  @input = [
9
- [ 'a', 'aa', 'b', 'ba' ],
10
- [ 'b', 'c', 'bc' ],
9
+ [ 'a', 'aa', 'b', 'ba', 'dd' ],
10
+ [ 'b', 'c', 'bc', 'de' ],
11
11
  ]
12
12
  end
13
13
 
14
14
  def test_stuff
15
- assert_equal([
16
- ['a', 'aa', 'ba'],
17
- [],
18
- ],
19
- @input.rekey(nil, /a/)
15
+ assert_equal({
16
+ 'a' => ['a', 'aa', 'ba'],
17
+ 'b' => [],
18
+ },
19
+ @input.rekey(0, /a/)
20
20
  )
21
21
 
22
- assert_equal([
23
- ['b', 'ba'],
24
- ['b', 'bc'],
25
- ],
26
- @input.rekey(nil, /b/)
22
+ assert_equal({
23
+ ['dd'] => ['b', 'ba'],
24
+ ['de'] => ['b', 'bc'],
25
+ },
26
+ @input.rekey(/d/, /b/)
27
27
  )
28
28
  end
29
29
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rekey
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-26 00:00:00.000000000 Z
11
+ date: 2018-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pluckit