pluckit 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pluckit.rb +23 -14
- data/test/test_array.rb +9 -0
- data/test/test_hash.rb +13 -0
- data/test/test_pluck.rb +21 -0
- data/test/test_set.rb +9 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42c6c3073a9907cc8cfc8dfe0fe43e4fe7a60fb5
|
4
|
+
data.tar.gz: 2c978a6bbd3d96e3c040af65cf85e4266f84041c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b18dcf896e35913022655730e469702cacad1c64e2aaffed5f1312edc223a1c735b027cf49ac7a7d19bed7737ca88fbf18120e61bb6bfa27f979051c499c66d
|
7
|
+
data.tar.gz: 4b31395eaaec78888732784450b796885353e3b0b200cac26c3fc5ce510d3a90ab6bedaeb418a67f3699e390065ec94219f97c0c5609bd2a9de5cfc1739f9a70
|
data/lib/pluckit.rb
CHANGED
@@ -1,19 +1,28 @@
|
|
1
1
|
module PluckIt
|
2
|
-
VERSION = '0.
|
2
|
+
VERSION = '0.2.0'
|
3
3
|
|
4
4
|
class << self
|
5
5
|
|
6
|
-
def pluck v,
|
6
|
+
def pluck v, *handles
|
7
|
+
if handles.count > 1
|
8
|
+
handles.map {|h| pluck_single v, h }
|
9
|
+
else
|
10
|
+
pluck_single v, handles.first
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def pluck_single v, handle
|
7
18
|
if v.is_a? Hash
|
8
19
|
v[handle]
|
9
20
|
elsif ([Symbol, String].include? handle.class) and v.respond_to? handle
|
10
21
|
v.send handle
|
11
|
-
elsif
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
v[handle]
|
16
|
-
end
|
22
|
+
elsif handle.is_a? Regexp and v.respond_to? :grep
|
23
|
+
v.grep handle
|
24
|
+
elsif v.respond_to? :[]
|
25
|
+
v[handle]
|
17
26
|
else
|
18
27
|
raise ArgumentError.new "invalid handle: #{handle}, for value #{v}"
|
19
28
|
end
|
@@ -24,27 +33,27 @@ end
|
|
24
33
|
|
25
34
|
|
26
35
|
class Array
|
27
|
-
def pluck
|
36
|
+
def pluck *handles
|
28
37
|
each_with_object(clone.clear) do |val, res|
|
29
|
-
res << PluckIt.pluck(val,
|
38
|
+
res << PluckIt.pluck(val, *handles)
|
30
39
|
end
|
31
40
|
end
|
32
41
|
end
|
33
42
|
|
34
43
|
|
35
44
|
class Hash
|
36
|
-
def pluck
|
45
|
+
def pluck *handles
|
37
46
|
each_with_object(clone.clear) do |(key, val), res|
|
38
|
-
res[key] = PluckIt.pluck(val,
|
47
|
+
res[key] = PluckIt.pluck(val, *handles)
|
39
48
|
end
|
40
49
|
end
|
41
50
|
end
|
42
51
|
|
43
52
|
|
44
53
|
class Set
|
45
|
-
def pluck
|
54
|
+
def pluck *handles
|
46
55
|
each_with_object(clone.clear) do |val, res|
|
47
|
-
res.add PluckIt.pluck(val,
|
56
|
+
res.add PluckIt.pluck(val, *handles)
|
48
57
|
end
|
49
58
|
end
|
50
59
|
end
|
data/test/test_array.rb
CHANGED
data/test/test_hash.rb
CHANGED
@@ -31,6 +31,19 @@ class PluckItHashTest < Minitest::Test
|
|
31
31
|
c: { x: 3, y: 6 },
|
32
32
|
}.pluck(:x)
|
33
33
|
)
|
34
|
+
|
35
|
+
assert_equal(
|
36
|
+
{
|
37
|
+
a: [ 1, 2 ],
|
38
|
+
b: [ 2, 4 ],
|
39
|
+
c: [ 3, 6 ],
|
40
|
+
},
|
41
|
+
{
|
42
|
+
a: { x: 1, y: 2 },
|
43
|
+
b: { x: 2, y: 4 },
|
44
|
+
c: { x: 3, y: 6 },
|
45
|
+
}.pluck(:x, :y)
|
46
|
+
)
|
34
47
|
end
|
35
48
|
|
36
49
|
|
data/test/test_pluck.rb
CHANGED
@@ -40,6 +40,11 @@ class PluckItTest < Minitest::Test
|
|
40
40
|
/a/
|
41
41
|
)
|
42
42
|
)
|
43
|
+
|
44
|
+
assert_equal(
|
45
|
+
[ 1, 2 ],
|
46
|
+
PluckIt.pluck(data, 0, 1)
|
47
|
+
)
|
43
48
|
end
|
44
49
|
|
45
50
|
|
@@ -62,11 +67,17 @@ class PluckItTest < Minitest::Test
|
|
62
67
|
assert_nil(
|
63
68
|
PluckIt.pluck(data, :count)
|
64
69
|
)
|
70
|
+
|
71
|
+
assert_equal(
|
72
|
+
[ 1, 2 ],
|
73
|
+
PluckIt.pluck(data, :a, :b)
|
74
|
+
)
|
65
75
|
end
|
66
76
|
|
67
77
|
|
68
78
|
class ABC
|
69
79
|
def foo() 123 end
|
80
|
+
def bar() 456 end
|
70
81
|
end
|
71
82
|
|
72
83
|
def test_obj
|
@@ -79,6 +90,11 @@ class PluckItTest < Minitest::Test
|
|
79
90
|
self.class.const_get(:ABC),
|
80
91
|
PluckIt.pluck(ABC.new, :class)
|
81
92
|
)
|
93
|
+
|
94
|
+
assert_equal(
|
95
|
+
[ 123, 456 ],
|
96
|
+
PluckIt.pluck(ABC.new, :foo, :bar)
|
97
|
+
)
|
82
98
|
end
|
83
99
|
|
84
100
|
|
@@ -94,6 +110,11 @@ class PluckItTest < Minitest::Test
|
|
94
110
|
3,
|
95
111
|
PluckIt.pluck(data, :count)
|
96
112
|
)
|
113
|
+
|
114
|
+
assert_equal(
|
115
|
+
[ 1, 3 ],
|
116
|
+
PluckIt.pluck(data, :first, :count)
|
117
|
+
)
|
97
118
|
end
|
98
119
|
|
99
120
|
|
data/test/test_set.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pluckit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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-
|
11
|
+
date: 2017-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
69
|
version: '0'
|
70
70
|
requirements: []
|
71
71
|
rubyforge_project:
|
72
|
-
rubygems_version: 2.6.
|
72
|
+
rubygems_version: 2.6.11
|
73
73
|
signing_key:
|
74
74
|
specification_version: 4
|
75
75
|
summary: PluckIt
|