rekey 1.0.0 → 1.1.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: a6c9d60055fe41b5eb0256261b47ddb024c9f59a
4
- data.tar.gz: aa50ed7dae9056378b866e0087c47f05924864f0
3
+ metadata.gz: 4d0928904addc15f172c1ca0045615ff80deef47
4
+ data.tar.gz: 25fe2c483347fe26e163b24669e4f6d8e36410ec
5
5
  SHA512:
6
- metadata.gz: e4d5f92fec88e352f45cb44a964e18ba305b2ebb2cc7e345e66e712382413791d30d5331499c6bbad6034260de97891b7973a550390d59593a363b3aaea077c9
7
- data.tar.gz: 62e709ab09d097bff692620e27ba1f8f32aeb062d517b3c05f19a424d545b4cde05c4bbe33010a2aa911a25af38d1545d5b8ceb4e6fc37bf13a2a87f9e90e79e
6
+ metadata.gz: e9460e299ab62557929c865860255e4e0e350297e55b2bf1bf1af90ae0eb3ca6f33c2cc90408590af69a8d86b1ff2e080419382df178d366cccc43435bb15889
7
+ data.tar.gz: fb96b3a2bbc086c41ebe620a127c6922e09addff5e9c3dc0e081132852d506ec9f0557632c0a6e7d70ac626294bc80e0ee62cb8b81984a35d582e2c7831605e9
data/lib/rekey.rb CHANGED
@@ -3,15 +3,7 @@ module Rekey
3
3
 
4
4
  def rekey(enumerable, key_handle = nil, value_handle = nil, &block)
5
5
  # validate input
6
- if block
7
- if (key_handle or value_handle)
8
- raise ArgumentError.new 'expected key / value handles, *or* block'
9
- end
10
- else
11
- unless key_handle or value_handle
12
- raise ArgumentError.new 'expected 1 or 2 args, got 0'
13
- end
14
- end
6
+ validate_input key_handle, value_handle, &block
15
7
 
16
8
  key_fn = if enumerable.respond_to?(:keys)
17
9
  proc {|k, v| k}
@@ -25,9 +17,9 @@ module Rekey
25
17
  proc {|v| v}
26
18
  end
27
19
 
28
- # return type will be determined later
29
- res = nil
20
+ res = get_return_type enumerable, key_handle, value_handle, &block
30
21
 
22
+ # rekey input
31
23
  enumerable.each do |*args|
32
24
  key = key_fn.call *args
33
25
  value = value_fn.call *args
@@ -83,6 +75,42 @@ module Rekey
83
75
 
84
76
  private
85
77
 
78
+ def validate_input key_handle, value_handle, &block
79
+ if block
80
+ if (key_handle or value_handle)
81
+ raise ArgumentError.new 'expected key / value handles, *or* block'
82
+ end
83
+ else
84
+ unless key_handle or value_handle
85
+ raise ArgumentError.new 'expected 1 or 2 args, got 0'
86
+ end
87
+ end
88
+ end
89
+
90
+
91
+ def get_return_type enumerable, key_handle, value_handle, &block
92
+ # determine return type
93
+ res = if block
94
+ # no way to determine type...do it dynamically,
95
+ # based on block return type
96
+
97
+ if enumerable.empty?
98
+ raise TypeError.new(
99
+ 'unable to determine return type for empty input'
100
+ )
101
+ end
102
+
103
+ nil
104
+ else
105
+ if key_handle or enumerable.respond_to?(:keys)
106
+ {}
107
+ else
108
+ []
109
+ end
110
+ end
111
+ end
112
+
113
+
86
114
  def pull v, handle
87
115
  if ([Symbol, String].include? handle.class) and v.respond_to? handle
88
116
  if v.method(handle).arity <= 0
data/lib/rekey/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rekey
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -0,0 +1,33 @@
1
+ require 'minitest/autorun'
2
+ require 'rekey'
3
+
4
+
5
+ class RekeyEmptyInputTest < Minitest::Test
6
+
7
+ def test_all
8
+ assert_equal(
9
+ {},
10
+ [].rekey(:k, :v)
11
+ )
12
+
13
+ assert_equal(
14
+ {},
15
+ {}.rekey(:k, :v)
16
+ )
17
+
18
+ assert_equal(
19
+ {},
20
+ Set.new.rekey(:k, :v)
21
+ )
22
+
23
+ assert_equal(
24
+ [],
25
+ Set.new.rekey(nil, :k)
26
+ )
27
+
28
+ assert_raises TypeError do
29
+ {}.rekey do |k| 1 end
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,25 @@
1
+ require 'minitest/autorun'
2
+ require 'rekey'
3
+
4
+
5
+ class RekeyInvalidInputTest < Minitest::Test
6
+ def setup
7
+ @input = [*1..3]
8
+ end
9
+
10
+
11
+ def test_all
12
+ assert_raises ArgumentError do
13
+ @input.rekey
14
+ end
15
+
16
+ assert_raises ArgumentError do
17
+ @input.rekey(:k) do |k| 1 end
18
+ end
19
+
20
+ assert_raises ArgumentError do
21
+ @input.rekey(:k, :v) do |k| 1 end
22
+ end
23
+ end
24
+
25
+ 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: 1.0.0
4
+ version: 1.1.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: 2016-12-29 00:00:00.000000000 Z
11
+ date: 2017-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -39,8 +39,10 @@ files:
39
39
  - lib/rekey/version.rb
40
40
  - test/test_array_block.rb
41
41
  - test/test_array_handlers.rb
42
+ - test/test_empty_input.rb
42
43
  - test/test_hash_block.rb
43
44
  - test/test_hash_handlers.rb
45
+ - test/test_invalid_input.rb
44
46
  - test/test_range.rb
45
47
  - test/test_regexp.rb
46
48
  homepage: https://github.com/d1hotpep/rekey
@@ -70,7 +72,9 @@ summary: Rekey
70
72
  test_files:
71
73
  - test/test_array_block.rb
72
74
  - test/test_array_handlers.rb
75
+ - test/test_empty_input.rb
73
76
  - test/test_hash_block.rb
74
77
  - test/test_hash_handlers.rb
78
+ - test/test_invalid_input.rb
75
79
  - test/test_range.rb
76
80
  - test/test_regexp.rb