rekey 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/rekey/version.rb +3 -0
- data/lib/rekey.rb +96 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aef7b449f66795bace280c82a74dc2ec9b6fb0e9
|
4
|
+
data.tar.gz: 00ce3968fcfa316c3f9f49279e26b46c0bd30d8c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 23b83545770104e3f968e1dfcf7ae3a503a87cce2b9452773bd4edec3b0da3ea3d380de358aea09aaa625e3fbfafbf53a858cb2bc1851f10d176886641af4c51
|
7
|
+
data.tar.gz: b78c2be0c10cfc6f56594b08987208433a7b2b2f1b255296bc996f7cc1bfea7614934095a08455a14b4dcb6d6c86479e4b7a9c035455b3579a483870129c9f03
|
data/lib/rekey.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'awesome_print'
|
2
|
+
require 'byebug'
|
3
|
+
|
4
|
+
module Rekey
|
5
|
+
class << self
|
6
|
+
|
7
|
+
def rekey(enumerable, key_handle = nil, value_handle = nil, &block)
|
8
|
+
# validate input
|
9
|
+
if block
|
10
|
+
if (key_handle or value_handle)
|
11
|
+
raise ArgumentError.new 'expected key / value handles, *or* block'
|
12
|
+
end
|
13
|
+
else
|
14
|
+
unless key_handle or value_handle
|
15
|
+
raise ArgumentError.new 'expected key_handle or value_handle'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
key_fn = if enumerable.respond_to?(:keys)
|
20
|
+
proc {|k, v| k}
|
21
|
+
else
|
22
|
+
proc {|v| nil}
|
23
|
+
end
|
24
|
+
|
25
|
+
value_fn = if enumerable.respond_to?(:values)
|
26
|
+
proc {|k, v| v}
|
27
|
+
else
|
28
|
+
proc {|v| v}
|
29
|
+
end
|
30
|
+
|
31
|
+
res = nil
|
32
|
+
# determine return type. generally a Hash, except when input
|
33
|
+
# is an Array and key_handle is null
|
34
|
+
if block
|
35
|
+
res = {}
|
36
|
+
else
|
37
|
+
res = (key_handle or enumerable.respond_to?(:keys)) ? {} : []
|
38
|
+
end
|
39
|
+
|
40
|
+
enumerable.each do |*args|
|
41
|
+
key = key_fn.call *args
|
42
|
+
value = value_fn.call *args
|
43
|
+
new_key = key
|
44
|
+
new_value = value
|
45
|
+
|
46
|
+
if block
|
47
|
+
if block.arity <= 0
|
48
|
+
# function pointer
|
49
|
+
new_key = block.call value
|
50
|
+
elsif block.arity == 1
|
51
|
+
# standard block
|
52
|
+
new_key = block.call value
|
53
|
+
else
|
54
|
+
new_key, new_value = block.call key, value
|
55
|
+
end
|
56
|
+
else
|
57
|
+
new_key = pull value, key_handle if key_handle
|
58
|
+
new_value = pull value, value_handle if value_handle
|
59
|
+
end
|
60
|
+
|
61
|
+
# collect results
|
62
|
+
if res.is_a? Array
|
63
|
+
res.push new_value
|
64
|
+
else
|
65
|
+
res[new_key] = new_value
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
res
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def pull v, handle
|
76
|
+
# byebug
|
77
|
+
if v.respond_to? handle
|
78
|
+
if v.method(handle).arity <= 0
|
79
|
+
v.send handle
|
80
|
+
else
|
81
|
+
v.send handle, v
|
82
|
+
end
|
83
|
+
else
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
module Enumerable
|
93
|
+
def rekey(key_handle = nil, value_handle = nil, &block)
|
94
|
+
Rekey.rekey self, key_handle, value_handle, &block
|
95
|
+
end
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rekey
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Pepper
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |2
|
14
|
+
For turning Arrays into Hashes, with keys computed from the Array
|
15
|
+
contents. Or for rekeying Hashes.
|
16
|
+
|
17
|
+
[ 1, 2, 3 ].rekey {|v| v}
|
18
|
+
=> { 1 => 1, 2 => 2, 3 => 3 }
|
19
|
+
email:
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- lib/rekey.rb
|
25
|
+
- lib/rekey/version.rb
|
26
|
+
homepage: https://github.com/d1hotpep/rekey
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.5.1
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Rekey
|
50
|
+
test_files: []
|