pluckit 1.0.0 → 1.1.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: 7587bb8c7e758bd2badc023cf95087b397386c67
4
- data.tar.gz: 0e230a36351043c05e174d0971ab28b259fca86f
3
+ metadata.gz: 1bd175a9c3fc863b62c01f1ef2b2a502f6c48f8f
4
+ data.tar.gz: 310068b14b9decdbfd8a49d3791b7427e135d2e8
5
5
  SHA512:
6
- metadata.gz: 1c0fd9d9d4e9dfaf4ffbf4699ba33105e027392b9f7a484519aacf3c065482adf6ab4e2e3979e5dfa0b6f3f2906f7890eb46ed8a5194f3301343a5a923d2a699
7
- data.tar.gz: 7989512d5bb2373eacdef52c87c011c7953a9350472e795fee65a5f7f61c04c1155efd71b9bbdbed760635fc07d98515813ccf70072baefaa958dd58aaffc3ab
6
+ metadata.gz: 35b8e21af1bcdb9b53dd9a42948b8716e1aca1cdf17a1d9623f9813b41526386d200999d5e97a7f2cdb0ad7403c8baa2948919f0093bc175b387f6b37fd1c213
7
+ data.tar.gz: 27e5d57fbdee0b69dfb8eeddd28304d1b76776ed42270e968a81240d3236a663f4c978c67f6c3e7904db1d997efa1673e4dd0118bbd39114f8e69937dd2ea321
@@ -1,33 +1,27 @@
1
1
  require_relative 'pluckit/pluck.rb'
2
2
 
3
3
  module PluckIt
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
6
6
 
7
7
 
8
8
 
9
9
  class Array
10
10
  def pluck *handles
11
- each_with_object(clone.clear) do |val, res|
12
- res << PluckIt.pluck(val, *handles)
13
- end
11
+ PluckIt.pluck self, *handles
14
12
  end
15
13
  end
16
14
 
17
15
 
18
16
  class Hash
19
17
  def pluck *handles
20
- each_with_object(clone.clear) do |(key, val), res|
21
- res[key] = PluckIt.pluck(val, *handles)
22
- end
18
+ PluckIt.pluck self, *handles
23
19
  end
24
20
  end
25
21
 
26
22
 
27
23
  class Set
28
24
  def pluck *handles
29
- each_with_object(clone.clear) do |val, res|
30
- res.add PluckIt.pluck(val, *handles)
31
- end
25
+ PluckIt.pluck self, *handles
32
26
  end
33
27
  end
@@ -1,18 +1,41 @@
1
1
  module PluckIt
2
2
  class << self
3
3
 
4
- def pluck v, *handles
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
25
+
26
+
27
+ def pluckit v, *handles
5
28
  if handles.count > 1
6
- handles.map {|h| pluck_single v, h }
29
+ handles.map {|h| pluckit_single v, h }
7
30
  else
8
- pluck_single v, handles.first
31
+ pluckit_single v, handles.first
9
32
  end
10
33
  end
11
34
 
12
35
 
13
36
  private
14
37
 
15
- def pluck_single v, handle
38
+ def pluckit_single v, handle
16
39
  if v.is_a? Hash
17
40
  v[handle]
18
41
  elsif ([Symbol, String].include? handle.class) and v.respond_to? handle
@@ -0,0 +1,36 @@
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
@@ -1,147 +1,52 @@
1
1
  require 'minitest/autorun'
2
2
  require 'set'
3
3
 
4
- $LOAD_PATH.unshift 'lib'
5
4
  require 'pluckit/pluck'
6
5
 
7
6
 
8
- class PluckItTest < Minitest::Test
7
+ """
8
+ basic sanity check, since other tests will
9
+ probe much more extensively
10
+ """
9
11
 
10
- def test_array
11
- data = [ 1, 2, 3 ]
12
+ class PluckTest < Minitest::Test
12
13
 
14
+ def test_basic
15
+ data = [
16
+ { k: 'a', v: 1 },
17
+ { k: 'b', v: 2 },
18
+ { k: 'c', v: 3 },
19
+ ]
13
20
  assert_equal(
14
- 1,
15
- PluckIt.pluck(data, 0)
21
+ [ 'a', 'b', 'c' ],
22
+ PluckIt.pluck(data, :k)
16
23
  )
17
24
 
18
- assert_equal(
19
- 2,
20
- PluckIt.pluck(data, 1)
21
- )
22
-
23
- assert_equal(
24
- 3,
25
- PluckIt.pluck(data, :last)
26
- )
27
-
28
- assert_equal(
29
- 3,
30
- PluckIt.pluck(data, :count)
31
- )
32
-
33
- assert_equal(
34
- [ 1, 2 ],
35
- PluckIt.pluck(data, 0..1)
36
- )
37
-
38
- assert_equal(
39
- [ 'a', 'ba', 'ca' ],
40
- PluckIt.pluck(
41
- [ 'a', 'ba', 'bb', 'bc', 'ca' ],
42
- /a/
43
- )
44
- )
45
25
 
46
- assert_equal(
47
- [ 1, 2 ],
48
- PluckIt.pluck(data, 0, 1)
49
- )
50
- end
51
-
52
-
53
- def test_hash
54
- data = {
55
- a: 1,
56
- b: 2,
57
- c: 3,
26
+ temp = {
27
+ june: [ 78, 82, 80 ],
28
+ july: [ 80, 83, 86 ],
29
+ august: [ 80, 76, 79 ],
58
30
  }
59
-
60
- assert_equal(
61
- 2,
62
- PluckIt.pluck(data, :b)
63
- )
64
-
65
- assert_nil(
66
- PluckIt.pluck(data, :z)
67
- )
68
-
69
- assert_nil(
70
- PluckIt.pluck(data, :count)
71
- )
72
-
73
31
  assert_equal(
74
- [ 1, 2 ],
75
- PluckIt.pluck(data, :a, :b)
76
- )
77
- end
78
-
79
-
80
- class ABC
81
- def foo() 123 end
82
- def bar() 456 end
83
- end
84
-
85
- def test_obj
86
- assert_equal(
87
- 123,
88
- PluckIt.pluck(ABC.new, :foo)
32
+ {
33
+ june: 82,
34
+ july: 86,
35
+ august: 80,
36
+ },
37
+ PluckIt.pluck(temp, :max)
89
38
  )
90
39
 
91
- assert_equal(
92
- self.class.const_get(:ABC),
93
- PluckIt.pluck(ABC.new, :class)
94
- )
95
40
 
41
+ people = Set.new([
42
+ {name: 'dpepper', age: 31},
43
+ {name: 'josh', age: 30},
44
+ ])
96
45
  assert_equal(
97
- [ 123, 456 ],
98
- PluckIt.pluck(ABC.new, :foo, :bar)
46
+ Set.new(['dpepper', 'josh']),
47
+ PluckIt.pluck(people, :name)
99
48
  )
100
49
  end
101
50
 
102
51
 
103
- def test_set
104
- data = Set.new [ 1, 2, 3 ]
105
-
106
- assert_equal(
107
- 1,
108
- PluckIt.pluck(data, :first)
109
- )
110
-
111
- assert_equal(
112
- 3,
113
- PluckIt.pluck(data, :count)
114
- )
115
-
116
- assert_equal(
117
- [ 1, 3 ],
118
- PluckIt.pluck(data, :first, :count)
119
- )
120
- end
121
-
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
-
147
52
  end
@@ -0,0 +1,122 @@
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pluckit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
@@ -48,7 +48,9 @@ files:
48
48
  - lib/pluckit/pluck.rb
49
49
  - test/test_array.rb
50
50
  - test/test_hash.rb
51
+ - test/test_monkey_patching.rb
51
52
  - test/test_pluck.rb
53
+ - test/test_pluckit.rb
52
54
  - test/test_set.rb
53
55
  homepage: https://github.com/dpep/rb_pluckit
54
56
  licenses:
@@ -77,5 +79,7 @@ summary: PluckIt
77
79
  test_files:
78
80
  - test/test_array.rb
79
81
  - test/test_hash.rb
82
+ - test/test_monkey_patching.rb
80
83
  - test/test_pluck.rb
84
+ - test/test_pluckit.rb
81
85
  - test/test_set.rb