pluckit 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f60041a9e7e6bf150d5f7a8d9cdaa72b795650b
4
- data.tar.gz: fd72fe2ed30100f8e5a07d1fe531d41153b91bb9
3
+ metadata.gz: 42c6c3073a9907cc8cfc8dfe0fe43e4fe7a60fb5
4
+ data.tar.gz: 2c978a6bbd3d96e3c040af65cf85e4266f84041c
5
5
  SHA512:
6
- metadata.gz: d61d2afa5f0b1308cf2c47c88a317b16174f81cb54cdd87e2104290c85789ad062c0447a77f1c2220389e82aa1df026396534caa3879cf702f5dcdce3a8d42f9
7
- data.tar.gz: 78982ef586f5c3b8b3b202715c25a73d09d8be9c6df6fe9ee47cc71778941ab49d18ee7c0922453c036f492995e0ae3b5909bff4bd7d7b1c09beb3843d6e6c31
6
+ metadata.gz: 5b18dcf896e35913022655730e469702cacad1c64e2aaffed5f1312edc223a1c735b027cf49ac7a7d19bed7737ca88fbf18120e61bb6bfa27f979051c499c66d
7
+ data.tar.gz: 4b31395eaaec78888732784450b796885353e3b0b200cac26c3fc5ce510d3a90ab6bedaeb418a67f3699e390065ec94219f97c0c5609bd2a9de5cfc1739f9a70
@@ -1,19 +1,28 @@
1
1
  module PluckIt
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
 
4
4
  class << self
5
5
 
6
- def pluck v, handle
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 v.is_a? Array
12
- if handle.is_a? Regexp
13
- v.grep handle
14
- else
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(handle)
36
+ def pluck *handles
28
37
  each_with_object(clone.clear) do |val, res|
29
- res << PluckIt.pluck(val, handle)
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(handle)
45
+ def pluck *handles
37
46
  each_with_object(clone.clear) do |(key, val), res|
38
- res[key] = PluckIt.pluck(val, handle)
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(handle)
54
+ def pluck *handles
46
55
  each_with_object(clone.clear) do |val, res|
47
- res.add PluckIt.pluck(val, handle)
56
+ res.add PluckIt.pluck(val, *handles)
48
57
  end
49
58
  end
50
59
  end
@@ -21,6 +21,15 @@ class PluckItArrayTest < Minitest::Test
21
21
  [ 3, 6, 9 ],
22
22
  data.pluck(:last)
23
23
  )
24
+
25
+ assert_equal(
26
+ [
27
+ [ 1, 2 ],
28
+ [ 4, 5 ],
29
+ [ 7, 8 ],
30
+ ],
31
+ data.pluck(0, 1)
32
+ )
24
33
  end
25
34
 
26
35
 
@@ -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
 
@@ -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
 
@@ -21,6 +21,15 @@ class PluckItSetTest < Minitest::Test
21
21
  Set.new([ 3, 6, 9 ]),
22
22
  data.pluck(:last)
23
23
  )
24
+
25
+ assert_equal(
26
+ Set.new([
27
+ [ 1, 3 ],
28
+ [ 4, 6 ],
29
+ [ 7, 9 ],
30
+ ]),
31
+ data.pluck(:first, :max)
32
+ )
24
33
  end
25
34
 
26
35
 
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.1.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-10-08 00:00:00.000000000 Z
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.13
72
+ rubygems_version: 2.6.11
73
73
  signing_key:
74
74
  specification_version: 4
75
75
  summary: PluckIt