pluckit 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pluckit.rb +12 -8
- data/test/test_array.rb +17 -10
- data/test/test_hash.rb +58 -0
- data/test/test_pluck.rb +71 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c06dc75e5f426dc4ad79cfab2c4d1728c29fc7b
|
4
|
+
data.tar.gz: 11c8dfcdb0b4a23aeecc091aa2e1caaa3dde37cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57943971842d5c0b76f0c14716d41040247e3de561e08f17b1634e08c3a648c250b75ec3a197a96a7b9564f06d6b05f2ae39633728ba593567bbb0ae18b382a9
|
7
|
+
data.tar.gz: c1433c1310c53811469c1b301b32a8375bacd150dc9b36debaa66fd0933abeb96a6c47507c4b912edfc55aa5f9db91ae38a090bb834c7d6bf8a708ace72f71e1
|
data/lib/pluckit.rb
CHANGED
@@ -1,16 +1,10 @@
|
|
1
1
|
module PluckIt
|
2
|
-
VERSION = '0.0.
|
2
|
+
VERSION = '0.0.2'
|
3
3
|
|
4
4
|
class << self
|
5
5
|
|
6
6
|
def pluck v, handle
|
7
|
-
if
|
8
|
-
if v.method(handle).arity <= 0
|
9
|
-
v.send handle
|
10
|
-
else
|
11
|
-
v.send handle, v
|
12
|
-
end
|
13
|
-
elsif v.is_a? Array
|
7
|
+
if v.is_a? Array
|
14
8
|
if handle.is_a? Regexp
|
15
9
|
v.grep handle
|
16
10
|
else
|
@@ -18,6 +12,8 @@ module PluckIt
|
|
18
12
|
end
|
19
13
|
elsif v.is_a? Hash
|
20
14
|
v[handle]
|
15
|
+
elsif ([Symbol, String].include? handle.class) and v.respond_to? handle
|
16
|
+
v.send handle
|
21
17
|
else
|
22
18
|
raise ArgumentError.new "invalid handle: #{handle}, for value #{v}"
|
23
19
|
end
|
@@ -32,3 +28,11 @@ class Array
|
|
32
28
|
map {|x| PluckIt.pluck x, handle }
|
33
29
|
end
|
34
30
|
end
|
31
|
+
|
32
|
+
|
33
|
+
class Hash
|
34
|
+
def pluck(handle)
|
35
|
+
res = values.map {|x| PluckIt.pluck x, handle }
|
36
|
+
Hash[keys.zip res]
|
37
|
+
end
|
38
|
+
end
|
data/test/test_array.rb
CHANGED
@@ -16,16 +16,6 @@ class PluckItArrayTest < Minitest::Test
|
|
16
16
|
[ 1, 4, 7 ],
|
17
17
|
data.pluck(0)
|
18
18
|
)
|
19
|
-
|
20
|
-
assert_equal(
|
21
|
-
[ 1, 4, 7 ],
|
22
|
-
data.pluck(:first)
|
23
|
-
)
|
24
|
-
|
25
|
-
assert_equal(
|
26
|
-
[ 1, 4, 7 ],
|
27
|
-
data.pluck('first')
|
28
|
-
)
|
29
19
|
end
|
30
20
|
|
31
21
|
|
@@ -69,4 +59,21 @@ class PluckItArrayTest < Minitest::Test
|
|
69
59
|
end
|
70
60
|
|
71
61
|
|
62
|
+
class ABC
|
63
|
+
attr_accessor :val
|
64
|
+
def initialize(v) self.val = v end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_obj
|
68
|
+
assert_equal(
|
69
|
+
[ 1, 2, 3 ],
|
70
|
+
[
|
71
|
+
ABC.new(1),
|
72
|
+
ABC.new(2),
|
73
|
+
ABC.new(3),
|
74
|
+
].pluck(:val)
|
75
|
+
)
|
76
|
+
end
|
77
|
+
|
78
|
+
|
72
79
|
end
|
data/test/test_hash.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'pluckit'
|
3
|
+
|
4
|
+
|
5
|
+
class PluckItHashTest < Minitest::Test
|
6
|
+
|
7
|
+
|
8
|
+
def test_basic
|
9
|
+
assert_equal(
|
10
|
+
{
|
11
|
+
a: 1,
|
12
|
+
b: 4,
|
13
|
+
c: 7,
|
14
|
+
},
|
15
|
+
{
|
16
|
+
a: [ 1, 2, 3 ],
|
17
|
+
b: [ 4, 5, 6 ],
|
18
|
+
c: [ 7, 8, 9 ],
|
19
|
+
}.pluck(0)
|
20
|
+
)
|
21
|
+
|
22
|
+
assert_equal(
|
23
|
+
{
|
24
|
+
a: 1,
|
25
|
+
b: 2,
|
26
|
+
c: 3,
|
27
|
+
},
|
28
|
+
{
|
29
|
+
a: { x: 1, y: 2 },
|
30
|
+
b: { x: 2, y: 4 },
|
31
|
+
c: { x: 3, y: 6 },
|
32
|
+
}.pluck(:x)
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
class ABC
|
38
|
+
attr_accessor :val
|
39
|
+
def initialize(v) self.val = v end
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_obj
|
43
|
+
assert_equal(
|
44
|
+
{
|
45
|
+
a: 1,
|
46
|
+
b: 2,
|
47
|
+
c: 3,
|
48
|
+
},
|
49
|
+
{
|
50
|
+
a: ABC.new(1),
|
51
|
+
b: ABC.new(2),
|
52
|
+
c: ABC.new(3),
|
53
|
+
}.pluck(:val)
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
end
|
data/test/test_pluck.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'pluckit'
|
3
|
+
|
4
|
+
|
5
|
+
class PluckItTest < Minitest::Test
|
6
|
+
|
7
|
+
|
8
|
+
def test_array
|
9
|
+
data = [ 1, 2, 3 ]
|
10
|
+
|
11
|
+
assert_equal(
|
12
|
+
1,
|
13
|
+
PluckIt.pluck(data, 0)
|
14
|
+
)
|
15
|
+
|
16
|
+
assert_equal(
|
17
|
+
2,
|
18
|
+
PluckIt.pluck(data, 1)
|
19
|
+
)
|
20
|
+
|
21
|
+
assert_equal(
|
22
|
+
[ 1, 2 ],
|
23
|
+
PluckIt.pluck(data, 0..1)
|
24
|
+
)
|
25
|
+
|
26
|
+
assert_equal(
|
27
|
+
[ 'a', 'ba', 'ca' ],
|
28
|
+
PluckIt.pluck(
|
29
|
+
[ 'a', 'ba', 'bb', 'bc', 'ca' ],
|
30
|
+
/a/
|
31
|
+
)
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def test_hash
|
37
|
+
data = {
|
38
|
+
a: 1,
|
39
|
+
b: 2,
|
40
|
+
c: 3,
|
41
|
+
}
|
42
|
+
|
43
|
+
assert_equal(
|
44
|
+
2,
|
45
|
+
PluckIt.pluck(data, :b)
|
46
|
+
)
|
47
|
+
|
48
|
+
assert_nil(
|
49
|
+
PluckIt.pluck(data, :z)
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
class ABC
|
55
|
+
def foo() 123 end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_obj
|
59
|
+
assert_equal(
|
60
|
+
123,
|
61
|
+
PluckIt.pluck(ABC.new, :foo)
|
62
|
+
)
|
63
|
+
|
64
|
+
assert_equal(
|
65
|
+
self.class.const_get(:ABC),
|
66
|
+
PluckIt.pluck(ABC.new, :class)
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pluckit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Pepper
|
@@ -46,6 +46,8 @@ extra_rdoc_files: []
|
|
46
46
|
files:
|
47
47
|
- lib/pluckit.rb
|
48
48
|
- test/test_array.rb
|
49
|
+
- test/test_hash.rb
|
50
|
+
- test/test_pluck.rb
|
49
51
|
homepage: https://github.com/dpep/pluckit
|
50
52
|
licenses:
|
51
53
|
- MIT
|
@@ -72,3 +74,5 @@ specification_version: 4
|
|
72
74
|
summary: PluckIt
|
73
75
|
test_files:
|
74
76
|
- test/test_array.rb
|
77
|
+
- test/test_hash.rb
|
78
|
+
- test/test_pluck.rb
|