rekey 2.0.1 → 3.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: 04e047b3df727cbbf23aec4f007c37b58dae87b4
4
- data.tar.gz: bf1636fc4e51c481a93d146d2710a287e5f41d24
3
+ metadata.gz: 3ca96460968d77a1bd8dd2c0ae8ccc75d1dd1a70
4
+ data.tar.gz: 3c66def36a31e036dcd2d655d96f2a0c89ae9cc8
5
5
  SHA512:
6
- metadata.gz: 89e5039519830eb14570f0446a021b4fb93b29a163263a224f9d2d1d343d07afed4f6a85b22bcbdc92c87a4e0943bc4728ca44ef0fd4feb90475e448226b5cb4
7
- data.tar.gz: 27ffe5a8550a6efbcd371e4715987e15af42484b0bef47b23df72f3cc8ff24f8dc8da25ccf887a913336412f566d64e5935831aae1cf6d1bb93fd2ead807ee71
6
+ metadata.gz: 4a93381b89113f30a4144eeba0e0a94c9127edf053d381a1c15981d24a410013343af7c00637742f32df6d64b3640ccb986f6f22db91b605785cee73a196055f
7
+ data.tar.gz: d88d014185adfc737ff1b3c9b66873d8c108a575e1fb1824007c0846c771600efb958ac5930279da4c19aabb47a7ff413913fe37d698d060c560dbfd9b4817ec
data/lib/rekey.rb CHANGED
@@ -1,122 +1,8 @@
1
- require 'pluckit'
1
+ require_relative 'rekey/rekey'
2
2
 
3
3
 
4
4
  module Rekey
5
- VERSION = '2.0.1'
6
-
7
- class << self
8
-
9
- def rekey(enumerable, key_handle = nil, value_handle = nil, &block)
10
- # validate input
11
- validate_input key_handle, value_handle, &block
12
-
13
- key_fn = if enumerable.respond_to?(:keys)
14
- proc {|k, v| k}
15
- else
16
- proc {|v| nil}
17
- end
18
-
19
- value_fn = if enumerable.respond_to?(:values)
20
- proc {|k, v| v}
21
- else
22
- proc {|v| v}
23
- end
24
-
25
- res = get_return_type enumerable, key_handle, value_handle, &block
26
-
27
- # rekey input
28
- enumerable.each do |*args|
29
- key = key_fn.call *args
30
- value = value_fn.call *args
31
- new_key = key
32
- new_value = value
33
-
34
- if block
35
- if block.arity <= 0
36
- # function pointer, eg. &:to_i
37
- new_key = block.call value
38
- elsif block.arity == 1
39
- # standard block
40
- new_key = block.call value
41
- else
42
- # block that wants both key and value
43
- data = block.call key, value
44
- if data.is_a? Array
45
- new_key, new_value = data
46
- else
47
- # only returned a new key value
48
- new_key = data
49
- end
50
- end
51
- else
52
- new_key = PluckIt.pluck value, key_handle if key_handle
53
- new_value = PluckIt.pluck value, value_handle if value_handle
54
- end
55
-
56
- # collect results
57
-
58
- unless res
59
- # determine return type based on the first
60
- # computed key value
61
- res = new_key ? {} : []
62
- end
63
-
64
- if res.is_a? Array
65
- unless new_key.nil?
66
- # safeguard against stupidity
67
- raise ArgumentError.new(
68
- "not expecting a key value, got: #{new_key}"
69
- )
70
- end
71
-
72
- res.push new_value
73
- else
74
- res[new_key] = new_value
75
- end
76
- end
77
-
78
- res
79
- end
80
-
81
-
82
- private
83
-
84
- def validate_input key_handle, value_handle, &block
85
- if block
86
- if (key_handle or value_handle)
87
- raise ArgumentError.new 'expected key / value handles, *or* block'
88
- end
89
- else
90
- unless key_handle or value_handle
91
- raise ArgumentError.new 'expected 1 or 2 args, got 0'
92
- end
93
- end
94
- end
95
-
96
-
97
- def get_return_type enumerable, key_handle, value_handle, &block
98
- # determine return type
99
- res = if block
100
- # no way to determine type...do it dynamically,
101
- # based on block return type
102
-
103
- if enumerable.empty?
104
- raise TypeError.new(
105
- 'unable to determine return type for empty input'
106
- )
107
- end
108
-
109
- nil
110
- else
111
- if key_handle or enumerable.respond_to?(:keys)
112
- {}
113
- else
114
- []
115
- end
116
- end
117
- end
118
-
119
- end
5
+ VERSION = '3.0.0'
120
6
  end
121
7
 
122
8
 
@@ -0,0 +1,78 @@
1
+ require 'pluckit/pluck'
2
+
3
+
4
+ module Rekey
5
+ class << self
6
+
7
+ def rekey(enumerable, key_handle = nil, value_handle = nil, &block)
8
+ validate_input key_handle, value_handle, &block
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
22
+
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
28
+
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
40
+ else
41
+ new_key = PluckIt.pluck value, key_handle if key_handle
42
+ new_value = PluckIt.pluck value, value_handle if value_handle
43
+ end
44
+
45
+ # collect results
46
+ if res.is_a? Array
47
+ unless new_key.nil?
48
+ # safeguard against stupidity
49
+ raise ArgumentError.new(
50
+ "not expecting a key value, got: #{new_key}"
51
+ )
52
+ end
53
+
54
+ res.push new_value
55
+ else
56
+ res[new_key] = new_value
57
+ end
58
+ end
59
+ end
60
+
61
+
62
+ private
63
+
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
73
+ end
74
+ end
75
+
76
+
77
+ end
78
+ end
@@ -8,24 +8,6 @@ class RekeyArrayBlockTest < 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 {|k, v| [v, v] }
18
- )
19
-
20
- assert_equal({
21
- '1' => 1.0,
22
- '2' => 2.0,
23
- '3' => 3.0,
24
- },
25
- @input.rekey {|k, v| [v.to_s, v.to_f] }
26
- )
27
- end
28
-
29
11
  def test_keys
30
12
  assert_equal({
31
13
  1 => 1,
@@ -54,22 +36,8 @@ class RekeyArrayBlockTest < Minitest::Test
54
36
 
55
37
  def test_nil_key
56
38
  assert_equal(
57
- @input,
58
- @input.rekey {|k, v| [nil, v.to_i] }
59
- )
60
-
61
- assert_equal(
62
- @input,
63
- # k is nil because input is array
64
- @input.rekey {|k, v| [k, v.to_i] }
65
- )
66
-
67
- assert_equal([
68
- '1',
69
- '2',
70
- '3',
71
- ],
72
- @input.rekey {|k, v| [nil, v.to_s] }
39
+ { nil => 3 },
40
+ @input.rekey {|k, v| nil }
73
41
  )
74
42
  end
75
43
 
@@ -24,10 +24,6 @@ class RekeyEmptyInputTest < Minitest::Test
24
24
  [],
25
25
  Set.new.rekey(nil, :k)
26
26
  )
27
-
28
- assert_raises TypeError do
29
- {}.rekey do |k| 1 end
30
- end
31
27
  end
32
28
 
33
29
  end
@@ -11,29 +11,6 @@ class RekeyHashBlockTest < Minitest::Test
11
11
  }
12
12
  end
13
13
 
14
- def test_both
15
- assert_equal(
16
- @input,
17
- @input.rekey {|k, v| [k, v] }
18
- )
19
-
20
- assert_equal({
21
- 1 => 1,
22
- 2 => 2,
23
- 3 => 3,
24
- },
25
- @input.rekey {|k, v| [v, v] }
26
- )
27
-
28
- assert_equal({
29
- '1' => 1.0,
30
- '2' => 2.0,
31
- '3' => 3.0,
32
- },
33
- @input.rekey {|k, v| [v.to_s, v.to_f] }
34
- )
35
- end
36
-
37
14
  def test_arity1_keys
38
15
  assert_equal({
39
16
  1 => 1,
@@ -88,12 +65,9 @@ class RekeyHashBlockTest < Minitest::Test
88
65
  end
89
66
 
90
67
  def test_nil_key
91
- assert_equal([
92
- 1,
93
- 2,
94
- 3,
95
- ],
96
- @input.rekey {|k, v| [nil, v] }
68
+ assert_equal(
69
+ { nil => 3 },
70
+ @input.rekey {|k, v| nil }
97
71
  )
98
72
  end
99
73
 
@@ -0,0 +1,34 @@
1
+ require 'minitest/autorun'
2
+ require 'set'
3
+
4
+ $LOAD_PATH.unshift 'lib'
5
+ require 'rekey/rekey'
6
+
7
+
8
+ class MonkeyPatchingTest < Minitest::Test
9
+ def test_not_installed
10
+ if defined? Rekey::VERSION
11
+ # entire library was loaded, so these tests are invalid.
12
+ # this happens when run through `rake`
13
+ skip
14
+ end
15
+
16
+ # `require 'pluckit/pluck'` should load PluckIt
17
+ # but not monkey patch
18
+ assert_raises NoMethodError do
19
+ [ 1, 2, 3 ].rekey 0
20
+ end
21
+
22
+ assert_raises NoMethodError do
23
+ { a: 1 }.rekey 0
24
+ end
25
+
26
+ assert_raises NoMethodError do
27
+ Set.new([ 1, 2, 3 ]).rekey 0
28
+ end
29
+
30
+ puts 'no monkey patching'
31
+ end
32
+
33
+
34
+ end
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: 2.0.1
4
+ version: 3.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: 2017-10-08 00:00:00.000000000 Z
11
+ date: 2018-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pluckit
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -59,15 +59,17 @@ extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
61
  - lib/rekey.rb
62
+ - lib/rekey/rekey.rb
62
63
  - test/test_array_block.rb
63
64
  - test/test_array_handlers.rb
64
65
  - test/test_empty_input.rb
65
66
  - test/test_hash_block.rb
66
67
  - test/test_hash_handlers.rb
67
68
  - test/test_invalid_input.rb
69
+ - test/test_monkey_patching.rb
68
70
  - test/test_range.rb
69
71
  - test/test_regexp.rb
70
- homepage: https://github.com/dpep/rekey
72
+ homepage: https://github.com/dpep/rb_rekey
71
73
  licenses:
72
74
  - MIT
73
75
  metadata: {}
@@ -87,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
89
  version: '0'
88
90
  requirements: []
89
91
  rubyforge_project:
90
- rubygems_version: 2.6.13
92
+ rubygems_version: 2.6.11
91
93
  signing_key:
92
94
  specification_version: 4
93
95
  summary: Rekey
@@ -98,5 +100,6 @@ test_files:
98
100
  - test/test_hash_block.rb
99
101
  - test/test_hash_handlers.rb
100
102
  - test/test_invalid_input.rb
103
+ - test/test_monkey_patching.rb
101
104
  - test/test_range.rb
102
105
  - test/test_regexp.rb