pluckit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/pluckit.rb +34 -0
  3. data/test/test_array.rb +72 -0
  4. metadata +74 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ab364fcad0f735ae899cd58285bedc88d04c4586
4
+ data.tar.gz: a61477d6eb298051c79f43797b3ab4d556479f1f
5
+ SHA512:
6
+ metadata.gz: ac883f33d3cb65eaccae40d1a1662367b9d785ed1eb87c075288517928c234d2a3e7ad96f8f595fed9fbc23364c5876a668ac616295ca767d1b6016a51161610
7
+ data.tar.gz: bf29eba3a2feeed14c17af96c39976038da4a4b8ce11794a20b7d5d3395b1b70cf2f4988c56122d76887e50e04c313458469d4dde6191534825a7739ec004a08
@@ -0,0 +1,34 @@
1
+ module PluckIt
2
+ VERSION = '0.0.1'
3
+
4
+ class << self
5
+
6
+ def pluck v, handle
7
+ if ([Symbol, String].include? handle.class) and v.respond_to? handle
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
14
+ if handle.is_a? Regexp
15
+ v.grep handle
16
+ else
17
+ v[handle]
18
+ end
19
+ elsif v.is_a? Hash
20
+ v[handle]
21
+ else
22
+ raise ArgumentError.new "invalid handle: #{handle}, for value #{v}"
23
+ end
24
+ end
25
+
26
+ end
27
+ end
28
+
29
+
30
+ class Array
31
+ def pluck(handle)
32
+ map {|x| PluckIt.pluck x, handle }
33
+ end
34
+ end
@@ -0,0 +1,72 @@
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
+ [ 1, 4, 7 ],
22
+ data.pluck(:first)
23
+ )
24
+
25
+ assert_equal(
26
+ [ 1, 4, 7 ],
27
+ data.pluck('first')
28
+ )
29
+ end
30
+
31
+
32
+ def test_empty
33
+ assert_equal(
34
+ [],
35
+ [].pluck(nil)
36
+ )
37
+
38
+ assert_equal(
39
+ [],
40
+ [].pluck(0)
41
+ )
42
+ end
43
+
44
+
45
+ def test_hashes
46
+ data = [
47
+ { k: 1, v: 2 },
48
+ { k: 2, v: 4 },
49
+ { k: 3, v: 6 },
50
+ ]
51
+
52
+ assert_equal(
53
+ [ 1, 2, 3 ],
54
+ data.pluck(:k)
55
+ )
56
+
57
+ assert_equal(
58
+ [ 2, 4, 6 ],
59
+ data.pluck(:v)
60
+ )
61
+ end
62
+
63
+
64
+ def test_missing
65
+ assert_equal(
66
+ [ nil, 2 ],
67
+ [{ a: 1 }, { k: 2 }].pluck(:k)
68
+ )
69
+ end
70
+
71
+
72
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pluckit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Pepper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10'
41
+ description: Extract values from Enumerables
42
+ email:
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/pluckit.rb
48
+ - test/test_array.rb
49
+ homepage: https://github.com/dpep/pluckit
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.6.13
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: PluckIt
73
+ test_files:
74
+ - test/test_array.rb