pluckit 1.1.0 → 1.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 +5 -5
- data/CHANGELOG.md +3 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +71 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/lib/pluckit/pluck.rb +54 -41
- data/lib/pluckit/version.rb +3 -0
- data/lib/pluckit.rb +3 -9
- data/pluckit.gemspec +21 -0
- metadata +50 -32
- data/test/test_array.rb +0 -109
- data/test/test_hash.rb +0 -93
- data/test/test_monkey_patching.rb +0 -36
- data/test/test_pluck.rb +0 -52
- data/test/test_pluckit.rb +0 -122
- data/test/test_set.rb +0 -52
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f0c8e686fd9ed75effb4ce6813bfa337ae81675547b5acacd5c40b2a6596cb69
|
|
4
|
+
data.tar.gz: 4dd1015875c89e0a4542e495316e82688cfa0ae64d1f1a67bcc9a040bc41586d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f190970663dce56184c3fdd91169899cb4a0db126d4fe543414ea41427d3061169bd131b860c23d617f0b99bd76af5617dcc8d513cb3cdfecc12d25a848c8b93
|
|
7
|
+
data.tar.gz: 8eb335b2b94d83a000ab27bc9198dd911736465005cee80134fd8752228b3c6e57ae355170a43f8e0c4be77b8529249ba5d8e2ebcceaf802511fbe09295b43de
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
pluckit (1.2.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
date (3.5.1)
|
|
10
|
+
debug (1.11.1)
|
|
11
|
+
irb (~> 1.10)
|
|
12
|
+
reline (>= 0.3.8)
|
|
13
|
+
diff-lcs (1.6.2)
|
|
14
|
+
docile (1.4.1)
|
|
15
|
+
erb (6.0.2)
|
|
16
|
+
io-console (0.8.2)
|
|
17
|
+
irb (1.17.0)
|
|
18
|
+
pp (>= 0.6.0)
|
|
19
|
+
prism (>= 1.3.0)
|
|
20
|
+
rdoc (>= 4.0.0)
|
|
21
|
+
reline (>= 0.4.2)
|
|
22
|
+
pp (0.6.3)
|
|
23
|
+
prettyprint
|
|
24
|
+
prettyprint (0.2.0)
|
|
25
|
+
prism (1.9.0)
|
|
26
|
+
psych (5.3.1)
|
|
27
|
+
date
|
|
28
|
+
stringio
|
|
29
|
+
rdoc (7.2.0)
|
|
30
|
+
erb
|
|
31
|
+
psych (>= 4.0.0)
|
|
32
|
+
tsort
|
|
33
|
+
reline (0.6.3)
|
|
34
|
+
io-console (~> 0.5)
|
|
35
|
+
rspec (3.13.2)
|
|
36
|
+
rspec-core (~> 3.13.0)
|
|
37
|
+
rspec-expectations (~> 3.13.0)
|
|
38
|
+
rspec-mocks (~> 3.13.0)
|
|
39
|
+
rspec-core (3.13.6)
|
|
40
|
+
rspec-support (~> 3.13.0)
|
|
41
|
+
rspec-debugging (0.0.4)
|
|
42
|
+
rspec-expectations (>= 3)
|
|
43
|
+
rspec-expectations (3.13.5)
|
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
45
|
+
rspec-support (~> 3.13.0)
|
|
46
|
+
rspec-mocks (3.13.8)
|
|
47
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
48
|
+
rspec-support (~> 3.13.0)
|
|
49
|
+
rspec-support (3.13.7)
|
|
50
|
+
simplecov (0.22.0)
|
|
51
|
+
docile (~> 1.1)
|
|
52
|
+
simplecov-html (~> 0.11)
|
|
53
|
+
simplecov_json_formatter (~> 0.1)
|
|
54
|
+
simplecov-html (0.13.2)
|
|
55
|
+
simplecov_json_formatter (0.1.4)
|
|
56
|
+
stringio (3.2.0)
|
|
57
|
+
tsort (0.2.0)
|
|
58
|
+
|
|
59
|
+
PLATFORMS
|
|
60
|
+
arm64-darwin-25
|
|
61
|
+
ruby
|
|
62
|
+
|
|
63
|
+
DEPENDENCIES
|
|
64
|
+
debug (>= 1)
|
|
65
|
+
pluckit!
|
|
66
|
+
rspec (>= 3.10)
|
|
67
|
+
rspec-debugging
|
|
68
|
+
simplecov (>= 0.22)
|
|
69
|
+
|
|
70
|
+
BUNDLED WITH
|
|
71
|
+
4.0.10
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Daniel Pepper
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
data/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
PluckIt
|
|
2
|
+
======
|
|
3
|
+

|
|
4
|
+
[](https://codecov.io/gh/dpep/pluckit_rb)
|
|
5
|
+
|
|
6
|
+
Extract values from Enumerables
|
|
7
|
+
|
|
8
|
+
#### Install
|
|
9
|
+
```gem install pluckit```
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
#### Usage
|
|
13
|
+
```ruby
|
|
14
|
+
require 'pluckit'
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# extract a field from a list of records
|
|
18
|
+
[
|
|
19
|
+
{ id: 1, name: 'alice', age: 30},
|
|
20
|
+
{ id: 2, name: 'bob', age: 24},
|
|
21
|
+
{ id: 3, name: 'charlie', age: 88},
|
|
22
|
+
].pluck :name
|
|
23
|
+
=> [ 'alice', 'bob', 'charlie' ]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# find max temp for each month
|
|
27
|
+
{
|
|
28
|
+
june: [ 78, 82, 80 ],
|
|
29
|
+
july: [ 80, 83, 86 ],
|
|
30
|
+
august: [ 80, 76, 79 ],
|
|
31
|
+
}.pluck :max
|
|
32
|
+
=> {
|
|
33
|
+
june: 82,
|
|
34
|
+
july: 86,
|
|
35
|
+
august: 80,
|
|
36
|
+
}
|
|
37
|
+
```
|
data/lib/pluckit/pluck.rb
CHANGED
|
@@ -1,53 +1,66 @@
|
|
|
1
1
|
module PluckIt
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
def pluck enumerable, *handles
|
|
6
|
-
items = enumerable.each_with_object(
|
|
7
|
-
enumerable.clone.clear
|
|
8
|
-
)
|
|
9
|
-
# TODO: if no clone / clear, use new
|
|
10
|
-
|
|
11
|
-
if enumerable.is_a?(Hash)
|
|
12
|
-
items.each do |(key, val), res|
|
|
13
|
-
res[key] = pluckit(val, *handles)
|
|
14
|
-
end
|
|
15
|
-
elsif enumerable.is_a?(Set)
|
|
16
|
-
items.each do |val, res|
|
|
17
|
-
res.add pluckit(val, *handles)
|
|
18
|
-
end
|
|
19
|
-
else
|
|
20
|
-
items.each do |val, res|
|
|
21
|
-
res << pluckit(val, *handles)
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
2
|
+
extend self
|
|
25
3
|
|
|
4
|
+
refine Array do
|
|
5
|
+
def pluck *handles
|
|
6
|
+
PluckIt.pluck self, *handles
|
|
7
|
+
end
|
|
8
|
+
end
|
|
26
9
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
else
|
|
31
|
-
pluckit_single v, handles.first
|
|
32
|
-
end
|
|
10
|
+
refine Hash do
|
|
11
|
+
def pluck *handles
|
|
12
|
+
PluckIt.pluck self, *handles
|
|
33
13
|
end
|
|
14
|
+
end
|
|
34
15
|
|
|
16
|
+
refine Set do
|
|
17
|
+
def pluck *handles
|
|
18
|
+
PluckIt.pluck self, *handles
|
|
19
|
+
end
|
|
20
|
+
end
|
|
35
21
|
|
|
36
|
-
|
|
22
|
+
def pluck enumerable, *handles
|
|
23
|
+
items = enumerable.each_with_object(
|
|
24
|
+
enumerable.clone.clear
|
|
25
|
+
)
|
|
26
|
+
# TODO: if no clone / clear, use new
|
|
37
27
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
elsif ([Symbol, String].include? handle.class) and v.respond_to? handle
|
|
42
|
-
v.send handle
|
|
43
|
-
elsif handle.is_a? Regexp and v.respond_to? :grep
|
|
44
|
-
v.grep handle
|
|
45
|
-
elsif v.respond_to? :[]
|
|
46
|
-
v[handle]
|
|
47
|
-
else
|
|
48
|
-
raise ArgumentError.new "invalid handle: #{handle}, for value #{v}"
|
|
28
|
+
if enumerable.is_a?(Hash)
|
|
29
|
+
items.each do |(key, val), res|
|
|
30
|
+
res[key] = pluckit(val, *handles)
|
|
49
31
|
end
|
|
32
|
+
elsif enumerable.is_a?(Set)
|
|
33
|
+
items.each do |val, res|
|
|
34
|
+
res.add pluckit(val, *handles)
|
|
35
|
+
end
|
|
36
|
+
else
|
|
37
|
+
items.each do |val, res|
|
|
38
|
+
res << pluckit(val, *handles)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def pluckit v, *handles
|
|
44
|
+
if handles.count > 1
|
|
45
|
+
handles.map {|h| pluckit_single v, h }
|
|
46
|
+
else
|
|
47
|
+
pluckit_single v, handles.first
|
|
50
48
|
end
|
|
49
|
+
end
|
|
51
50
|
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def pluckit_single v, handle
|
|
54
|
+
if v.is_a? Hash
|
|
55
|
+
v[handle]
|
|
56
|
+
elsif ([Symbol, String].include? handle.class) && v.respond_to?(handle)
|
|
57
|
+
v.send handle
|
|
58
|
+
elsif handle.is_a?(Regexp) && v.respond_to?(:grep)
|
|
59
|
+
v.grep handle
|
|
60
|
+
elsif v.respond_to?(:[])
|
|
61
|
+
v[handle]
|
|
62
|
+
else
|
|
63
|
+
raise ArgumentError.new "invalid handle: #{handle}, for value #{v}"
|
|
64
|
+
end
|
|
52
65
|
end
|
|
53
66
|
end
|
data/lib/pluckit.rb
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
VERSION = '1.1.0'
|
|
5
|
-
end
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
require 'set'
|
|
2
|
+
require_relative 'pluckit/version'
|
|
3
|
+
require_relative 'pluckit/pluck'
|
|
8
4
|
|
|
9
5
|
class Array
|
|
10
6
|
def pluck *handles
|
|
@@ -12,14 +8,12 @@ class Array
|
|
|
12
8
|
end
|
|
13
9
|
end
|
|
14
10
|
|
|
15
|
-
|
|
16
11
|
class Hash
|
|
17
12
|
def pluck *handles
|
|
18
13
|
PluckIt.pluck self, *handles
|
|
19
14
|
end
|
|
20
15
|
end
|
|
21
16
|
|
|
22
|
-
|
|
23
17
|
class Set
|
|
24
18
|
def pluck *handles
|
|
25
19
|
PluckIt.pluck self, *handles
|
data/pluckit.gemspec
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
package_name = File.basename(__FILE__, '.gemspec')
|
|
2
|
+
require_relative File.join('lib', package_name, 'version')
|
|
3
|
+
package = PluckIt
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |s|
|
|
6
|
+
s.description = 'Extract values from Enumerables'
|
|
7
|
+
s.authors = ['Daniel Pepper']
|
|
8
|
+
s.files = `git ls-files * ':!:spec'`.split("\n")
|
|
9
|
+
s.homepage = "https://github.com/dpep/rb_#{package_name}"
|
|
10
|
+
s.license = 'MIT'
|
|
11
|
+
s.name = package_name
|
|
12
|
+
s.summary = 'PluckIt'
|
|
13
|
+
s.version = package.const_get('VERSION')
|
|
14
|
+
|
|
15
|
+
s.required_ruby_version = '>= 3'
|
|
16
|
+
|
|
17
|
+
s.add_development_dependency 'debug', '>= 1'
|
|
18
|
+
s.add_development_dependency 'rspec', '>= 3.10'
|
|
19
|
+
s.add_development_dependency 'rspec-debugging'
|
|
20
|
+
s.add_development_dependency 'simplecov', '>= 0.22'
|
|
21
|
+
end
|
metadata
CHANGED
|
@@ -1,62 +1,88 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pluckit
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Daniel Pepper
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
13
|
+
name: debug
|
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
|
16
15
|
requirements:
|
|
17
|
-
- - "
|
|
16
|
+
- - ">="
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
18
|
+
version: '1'
|
|
20
19
|
type: :development
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
|
-
- - "
|
|
23
|
+
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
25
|
+
version: '1'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
|
-
name:
|
|
27
|
+
name: rspec
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
30
29
|
requirements:
|
|
31
|
-
- - "
|
|
30
|
+
- - ">="
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '10'
|
|
32
|
+
version: '3.10'
|
|
34
33
|
type: :development
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
|
-
- - "
|
|
37
|
+
- - ">="
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '10'
|
|
39
|
+
version: '3.10'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rspec-debugging
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: simplecov
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0.22'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0.22'
|
|
41
68
|
description: Extract values from Enumerables
|
|
42
|
-
email:
|
|
43
69
|
executables: []
|
|
44
70
|
extensions: []
|
|
45
71
|
extra_rdoc_files: []
|
|
46
72
|
files:
|
|
73
|
+
- CHANGELOG.md
|
|
74
|
+
- Gemfile
|
|
75
|
+
- Gemfile.lock
|
|
76
|
+
- LICENSE.txt
|
|
77
|
+
- README.md
|
|
47
78
|
- lib/pluckit.rb
|
|
48
79
|
- lib/pluckit/pluck.rb
|
|
49
|
-
-
|
|
50
|
-
-
|
|
51
|
-
- test/test_monkey_patching.rb
|
|
52
|
-
- test/test_pluck.rb
|
|
53
|
-
- test/test_pluckit.rb
|
|
54
|
-
- test/test_set.rb
|
|
80
|
+
- lib/pluckit/version.rb
|
|
81
|
+
- pluckit.gemspec
|
|
55
82
|
homepage: https://github.com/dpep/rb_pluckit
|
|
56
83
|
licenses:
|
|
57
84
|
- MIT
|
|
58
85
|
metadata: {}
|
|
59
|
-
post_install_message:
|
|
60
86
|
rdoc_options: []
|
|
61
87
|
require_paths:
|
|
62
88
|
- lib
|
|
@@ -64,22 +90,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
64
90
|
requirements:
|
|
65
91
|
- - ">="
|
|
66
92
|
- !ruby/object:Gem::Version
|
|
67
|
-
version: '
|
|
93
|
+
version: '3'
|
|
68
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
95
|
requirements:
|
|
70
96
|
- - ">="
|
|
71
97
|
- !ruby/object:Gem::Version
|
|
72
98
|
version: '0'
|
|
73
99
|
requirements: []
|
|
74
|
-
|
|
75
|
-
rubygems_version: 2.6.11
|
|
76
|
-
signing_key:
|
|
100
|
+
rubygems_version: 3.6.9
|
|
77
101
|
specification_version: 4
|
|
78
102
|
summary: PluckIt
|
|
79
|
-
test_files:
|
|
80
|
-
- test/test_array.rb
|
|
81
|
-
- test/test_hash.rb
|
|
82
|
-
- test/test_monkey_patching.rb
|
|
83
|
-
- test/test_pluck.rb
|
|
84
|
-
- test/test_pluckit.rb
|
|
85
|
-
- test/test_set.rb
|
|
103
|
+
test_files: []
|
data/test/test_array.rb
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
require 'minitest/autorun'
|
|
2
|
-
require 'pluckit'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class PluckItArrayTest < Minitest::Test
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def test_basic
|
|
9
|
-
data = [
|
|
10
|
-
[ 1, 2, 3 ],
|
|
11
|
-
[ 4, 5, 6 ],
|
|
12
|
-
[ 7, 8, 9 ],
|
|
13
|
-
]
|
|
14
|
-
|
|
15
|
-
assert_equal(
|
|
16
|
-
[ 1, 4, 7 ],
|
|
17
|
-
data.pluck(0)
|
|
18
|
-
)
|
|
19
|
-
|
|
20
|
-
assert_equal(
|
|
21
|
-
[ 3, 6, 9 ],
|
|
22
|
-
data.pluck(:last)
|
|
23
|
-
)
|
|
24
|
-
|
|
25
|
-
assert_equal(
|
|
26
|
-
[
|
|
27
|
-
[ 1, 2 ],
|
|
28
|
-
[ 4, 5 ],
|
|
29
|
-
[ 7, 8 ],
|
|
30
|
-
],
|
|
31
|
-
data.pluck(0, 1)
|
|
32
|
-
)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
def test_empty
|
|
37
|
-
assert_equal(
|
|
38
|
-
[],
|
|
39
|
-
[].pluck(nil)
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
assert_equal(
|
|
43
|
-
[],
|
|
44
|
-
[].pluck(0)
|
|
45
|
-
)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
def test_hashes
|
|
50
|
-
data = [
|
|
51
|
-
{ k: 1, v: 2 },
|
|
52
|
-
{ k: 2, v: 4 },
|
|
53
|
-
{ k: 3, v: 6 },
|
|
54
|
-
]
|
|
55
|
-
|
|
56
|
-
assert_equal(
|
|
57
|
-
[ 1, 2, 3 ],
|
|
58
|
-
data.pluck(:k)
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
assert_equal(
|
|
62
|
-
[ 2, 4, 6 ],
|
|
63
|
-
data.pluck(:v)
|
|
64
|
-
)
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
def test_missing
|
|
69
|
-
assert_equal(
|
|
70
|
-
[ nil, 2 ],
|
|
71
|
-
[{ a: 1 }, { k: 2 }].pluck(:k)
|
|
72
|
-
)
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
class ABC
|
|
77
|
-
attr_accessor :val
|
|
78
|
-
def initialize(v) self.val = v end
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def test_obj
|
|
82
|
-
assert_equal(
|
|
83
|
-
[ 1, 2, 3 ],
|
|
84
|
-
[
|
|
85
|
-
ABC.new(1),
|
|
86
|
-
ABC.new(2),
|
|
87
|
-
ABC.new(3),
|
|
88
|
-
].pluck(:val)
|
|
89
|
-
)
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
class MyArray < Array; end
|
|
94
|
-
def test_clone
|
|
95
|
-
data = MyArray.new [ 1, 2, 3 ]
|
|
96
|
-
|
|
97
|
-
assert_equal(
|
|
98
|
-
[ 1, 2, 3 ],
|
|
99
|
-
data.pluck(:itself)
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
assert_equal(
|
|
103
|
-
self.class.const_get(:MyArray),
|
|
104
|
-
data.pluck(:itself).class
|
|
105
|
-
)
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
end
|
data/test/test_hash.rb
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
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
|
-
|
|
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
|
-
)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
class ABC
|
|
51
|
-
attr_accessor :val
|
|
52
|
-
def initialize(v) self.val = v end
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def test_obj
|
|
56
|
-
assert_equal(
|
|
57
|
-
{
|
|
58
|
-
a: 1,
|
|
59
|
-
b: 2,
|
|
60
|
-
c: 3,
|
|
61
|
-
},
|
|
62
|
-
{
|
|
63
|
-
a: ABC.new(1),
|
|
64
|
-
b: ABC.new(2),
|
|
65
|
-
c: ABC.new(3),
|
|
66
|
-
}.pluck(:val)
|
|
67
|
-
)
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
class MyHash < Hash; end
|
|
72
|
-
def test_clone
|
|
73
|
-
data = MyHash.new 'abc'
|
|
74
|
-
data.update a: 1
|
|
75
|
-
|
|
76
|
-
assert_equal(
|
|
77
|
-
{ a: 1 },
|
|
78
|
-
data.pluck(:itself)
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
assert_equal(
|
|
82
|
-
'abc',
|
|
83
|
-
data.pluck(:itself)[:missing]
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
assert_equal(
|
|
87
|
-
self.class.const_get(:MyHash),
|
|
88
|
-
data.pluck(:itself).class
|
|
89
|
-
)
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
end
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
require 'minitest/autorun'
|
|
2
|
-
require 'set'
|
|
3
|
-
|
|
4
|
-
$LOAD_PATH.unshift 'lib'
|
|
5
|
-
require 'pluckit/pluck'
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class MonkeyPatchingTest < Minitest::Test
|
|
9
|
-
def test_not_installed
|
|
10
|
-
if defined? PluckIt::VERSION
|
|
11
|
-
# entire library was loaded, so these tests are invalid.
|
|
12
|
-
# this happens when run through `rake`
|
|
13
|
-
skip
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
# `require 'pluckit/pluck'` should load PluckIt
|
|
17
|
-
assert PluckIt
|
|
18
|
-
|
|
19
|
-
# but no monkey patching
|
|
20
|
-
assert_raises NoMethodError do
|
|
21
|
-
[ 1, 2, 3 ].pluck 0
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
assert_raises NoMethodError do
|
|
25
|
-
{ a: 1 }.pluck 0
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
assert_raises NoMethodError do
|
|
29
|
-
Set.new([ 1, 2, 3 ]).pluck 0
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
puts 'yay, no monkey patching'
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
end
|
data/test/test_pluck.rb
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
require 'minitest/autorun'
|
|
2
|
-
require 'set'
|
|
3
|
-
|
|
4
|
-
require 'pluckit/pluck'
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
"""
|
|
8
|
-
basic sanity check, since other tests will
|
|
9
|
-
probe much more extensively
|
|
10
|
-
"""
|
|
11
|
-
|
|
12
|
-
class PluckTest < Minitest::Test
|
|
13
|
-
|
|
14
|
-
def test_basic
|
|
15
|
-
data = [
|
|
16
|
-
{ k: 'a', v: 1 },
|
|
17
|
-
{ k: 'b', v: 2 },
|
|
18
|
-
{ k: 'c', v: 3 },
|
|
19
|
-
]
|
|
20
|
-
assert_equal(
|
|
21
|
-
[ 'a', 'b', 'c' ],
|
|
22
|
-
PluckIt.pluck(data, :k)
|
|
23
|
-
)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
temp = {
|
|
27
|
-
june: [ 78, 82, 80 ],
|
|
28
|
-
july: [ 80, 83, 86 ],
|
|
29
|
-
august: [ 80, 76, 79 ],
|
|
30
|
-
}
|
|
31
|
-
assert_equal(
|
|
32
|
-
{
|
|
33
|
-
june: 82,
|
|
34
|
-
july: 86,
|
|
35
|
-
august: 80,
|
|
36
|
-
},
|
|
37
|
-
PluckIt.pluck(temp, :max)
|
|
38
|
-
)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
people = Set.new([
|
|
42
|
-
{name: 'dpepper', age: 31},
|
|
43
|
-
{name: 'josh', age: 30},
|
|
44
|
-
])
|
|
45
|
-
assert_equal(
|
|
46
|
-
Set.new(['dpepper', 'josh']),
|
|
47
|
-
PluckIt.pluck(people, :name)
|
|
48
|
-
)
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
end
|
data/test/test_pluckit.rb
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
require 'minitest/autorun'
|
|
2
|
-
require 'set'
|
|
3
|
-
|
|
4
|
-
require 'pluckit/pluck'
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class PluckItTest < Minitest::Test
|
|
8
|
-
|
|
9
|
-
def test_array
|
|
10
|
-
data = [ 1, 2, 3 ]
|
|
11
|
-
|
|
12
|
-
assert_equal(
|
|
13
|
-
1,
|
|
14
|
-
PluckIt.pluckit(data, 0)
|
|
15
|
-
)
|
|
16
|
-
|
|
17
|
-
assert_equal(
|
|
18
|
-
2,
|
|
19
|
-
PluckIt.pluckit(data, 1)
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
assert_equal(
|
|
23
|
-
3,
|
|
24
|
-
PluckIt.pluckit(data, :last)
|
|
25
|
-
)
|
|
26
|
-
|
|
27
|
-
assert_equal(
|
|
28
|
-
3,
|
|
29
|
-
PluckIt.pluckit(data, :count)
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
assert_equal(
|
|
33
|
-
[ 1, 2 ],
|
|
34
|
-
PluckIt.pluckit(data, 0..1)
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
assert_equal(
|
|
38
|
-
[ 'a', 'ba', 'ca' ],
|
|
39
|
-
PluckIt.pluckit(
|
|
40
|
-
[ 'a', 'ba', 'bb', 'bc', 'ca' ],
|
|
41
|
-
/a/
|
|
42
|
-
)
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
assert_equal(
|
|
46
|
-
[ 1, 2 ],
|
|
47
|
-
PluckIt.pluckit(data, 0, 1)
|
|
48
|
-
)
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
def test_hash
|
|
53
|
-
data = {
|
|
54
|
-
a: 1,
|
|
55
|
-
b: 2,
|
|
56
|
-
c: 3,
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
assert_equal(
|
|
60
|
-
2,
|
|
61
|
-
PluckIt.pluckit(data, :b)
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
assert_nil(
|
|
65
|
-
PluckIt.pluckit(data, :z)
|
|
66
|
-
)
|
|
67
|
-
|
|
68
|
-
assert_nil(
|
|
69
|
-
PluckIt.pluckit(data, :count)
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
assert_equal(
|
|
73
|
-
[ 1, 2 ],
|
|
74
|
-
PluckIt.pluckit(data, :a, :b)
|
|
75
|
-
)
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
class ABC
|
|
80
|
-
def foo() 123 end
|
|
81
|
-
def bar() 456 end
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def test_obj
|
|
85
|
-
assert_equal(
|
|
86
|
-
123,
|
|
87
|
-
PluckIt.pluckit(ABC.new, :foo)
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
assert_equal(
|
|
91
|
-
self.class.const_get(:ABC),
|
|
92
|
-
PluckIt.pluckit(ABC.new, :class)
|
|
93
|
-
)
|
|
94
|
-
|
|
95
|
-
assert_equal(
|
|
96
|
-
[ 123, 456 ],
|
|
97
|
-
PluckIt.pluckit(ABC.new, :foo, :bar)
|
|
98
|
-
)
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
def test_set
|
|
103
|
-
data = Set.new [ 1, 2, 3 ]
|
|
104
|
-
|
|
105
|
-
assert_equal(
|
|
106
|
-
1,
|
|
107
|
-
PluckIt.pluckit(data, :first)
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
assert_equal(
|
|
111
|
-
3,
|
|
112
|
-
PluckIt.pluckit(data, :count)
|
|
113
|
-
)
|
|
114
|
-
|
|
115
|
-
assert_equal(
|
|
116
|
-
[ 1, 3 ],
|
|
117
|
-
PluckIt.pluckit(data, :first, :count)
|
|
118
|
-
)
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
end
|
data/test/test_set.rb
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
require 'minitest/autorun'
|
|
2
|
-
require 'pluckit'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class PluckItSetTest < Minitest::Test
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def test_basic
|
|
9
|
-
data = Set.new [
|
|
10
|
-
[ 1, 2, 3 ],
|
|
11
|
-
[ 4, 5, 6 ],
|
|
12
|
-
[ 7, 8, 9 ],
|
|
13
|
-
]
|
|
14
|
-
|
|
15
|
-
assert_equal(
|
|
16
|
-
Set.new([ 1, 4, 7 ]),
|
|
17
|
-
data.pluck(0)
|
|
18
|
-
)
|
|
19
|
-
|
|
20
|
-
assert_equal(
|
|
21
|
-
Set.new([ 3, 6, 9 ]),
|
|
22
|
-
data.pluck(:last)
|
|
23
|
-
)
|
|
24
|
-
|
|
25
|
-
assert_equal(
|
|
26
|
-
Set.new([
|
|
27
|
-
[ 1, 3 ],
|
|
28
|
-
[ 4, 6 ],
|
|
29
|
-
[ 7, 9 ],
|
|
30
|
-
]),
|
|
31
|
-
data.pluck(:first, :max)
|
|
32
|
-
)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
class MySet < Set; end
|
|
37
|
-
def test_clone
|
|
38
|
-
data = MySet.new [ 1, 2, 3 ]
|
|
39
|
-
|
|
40
|
-
assert_equal(
|
|
41
|
-
Set.new([ 1, 2, 3 ]),
|
|
42
|
-
data.pluck(:itself)
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
assert_equal(
|
|
46
|
-
self.class.const_get(:MySet),
|
|
47
|
-
data.pluck(:itself).class
|
|
48
|
-
)
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
end
|