pluckit 0.2.0 → 1.0.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 +4 -30
- data/lib/pluckit/pluck.rb +30 -0
- data/test/test_pluck.rb +28 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7587bb8c7e758bd2badc023cf95087b397386c67
|
4
|
+
data.tar.gz: 0e230a36351043c05e174d0971ab28b259fca86f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c0fd9d9d4e9dfaf4ffbf4699ba33105e027392b9f7a484519aacf3c065482adf6ab4e2e3979e5dfa0b6f3f2906f7890eb46ed8a5194f3301343a5a923d2a699
|
7
|
+
data.tar.gz: 7989512d5bb2373eacdef52c87c011c7953a9350472e795fee65a5f7f61c04c1155efd71b9bbdbed760635fc07d98515813ccf70072baefaa958dd58aaffc3ab
|
data/lib/pluckit.rb
CHANGED
@@ -1,37 +1,11 @@
|
|
1
|
-
|
2
|
-
VERSION = '0.2.0'
|
3
|
-
|
4
|
-
class << self
|
5
|
-
|
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
|
-
|
1
|
+
require_relative 'pluckit/pluck.rb'
|
14
2
|
|
15
|
-
|
16
|
-
|
17
|
-
def pluck_single v, handle
|
18
|
-
if v.is_a? Hash
|
19
|
-
v[handle]
|
20
|
-
elsif ([Symbol, String].include? handle.class) and v.respond_to? handle
|
21
|
-
v.send handle
|
22
|
-
elsif handle.is_a? Regexp and v.respond_to? :grep
|
23
|
-
v.grep handle
|
24
|
-
elsif v.respond_to? :[]
|
25
|
-
v[handle]
|
26
|
-
else
|
27
|
-
raise ArgumentError.new "invalid handle: #{handle}, for value #{v}"
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
3
|
+
module PluckIt
|
4
|
+
VERSION = '1.0.0'
|
32
5
|
end
|
33
6
|
|
34
7
|
|
8
|
+
|
35
9
|
class Array
|
36
10
|
def pluck *handles
|
37
11
|
each_with_object(clone.clear) do |val, res|
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module PluckIt
|
2
|
+
class << self
|
3
|
+
|
4
|
+
def pluck v, *handles
|
5
|
+
if handles.count > 1
|
6
|
+
handles.map {|h| pluck_single v, h }
|
7
|
+
else
|
8
|
+
pluck_single v, handles.first
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def pluck_single v, handle
|
16
|
+
if v.is_a? Hash
|
17
|
+
v[handle]
|
18
|
+
elsif ([Symbol, String].include? handle.class) and v.respond_to? handle
|
19
|
+
v.send handle
|
20
|
+
elsif handle.is_a? Regexp and v.respond_to? :grep
|
21
|
+
v.grep handle
|
22
|
+
elsif v.respond_to? :[]
|
23
|
+
v[handle]
|
24
|
+
else
|
25
|
+
raise ArgumentError.new "invalid handle: #{handle}, for value #{v}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/test/test_pluck.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'minitest/autorun'
|
2
|
-
require '
|
2
|
+
require 'set'
|
3
3
|
|
4
|
+
$LOAD_PATH.unshift 'lib'
|
5
|
+
require 'pluckit/pluck'
|
4
6
|
|
5
|
-
class PluckItTest < Minitest::Test
|
6
7
|
|
8
|
+
class PluckItTest < Minitest::Test
|
7
9
|
|
8
10
|
def test_array
|
9
11
|
data = [ 1, 2, 3 ]
|
@@ -118,4 +120,28 @@ class PluckItTest < Minitest::Test
|
|
118
120
|
end
|
119
121
|
|
120
122
|
|
123
|
+
def test_not_installed
|
124
|
+
if defined? PluckIt::VERSION
|
125
|
+
# entire library was loaded, so these tests are invalid.
|
126
|
+
# this happens when run through `rake`
|
127
|
+
return
|
128
|
+
end
|
129
|
+
|
130
|
+
# `require 'pluckit/pluck'` should load PluckIt
|
131
|
+
# but not monkey patch
|
132
|
+
assert_raises NoMethodError do
|
133
|
+
[ 1, 2, 3 ].pluck 0
|
134
|
+
end
|
135
|
+
|
136
|
+
assert_raises NoMethodError do
|
137
|
+
{ a: 1 }.pluck 0
|
138
|
+
end
|
139
|
+
|
140
|
+
assert_raises NoMethodError do
|
141
|
+
Set.new([ 1, 2, 3 ]).pluck 0
|
142
|
+
end
|
143
|
+
|
144
|
+
puts 'no monkey patching'
|
145
|
+
end
|
146
|
+
|
121
147
|
end
|
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: 1.0.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:
|
11
|
+
date: 2018-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -45,11 +45,12 @@ extensions: []
|
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
47
|
- lib/pluckit.rb
|
48
|
+
- lib/pluckit/pluck.rb
|
48
49
|
- test/test_array.rb
|
49
50
|
- test/test_hash.rb
|
50
51
|
- test/test_pluck.rb
|
51
52
|
- test/test_set.rb
|
52
|
-
homepage: https://github.com/dpep/
|
53
|
+
homepage: https://github.com/dpep/rb_pluckit
|
53
54
|
licenses:
|
54
55
|
- MIT
|
55
56
|
metadata: {}
|