grab 0.0.2 → 0.0.3
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.
- data/Rakefile +6 -0
- data/grab.gemspec +1 -0
- data/lib/grab.rb +16 -10
- data/lib/grab/version.rb +1 -1
- data/spec/grab_spec.rb +35 -0
- metadata +21 -3
data/Rakefile
CHANGED
data/grab.gemspec
CHANGED
data/lib/grab.rb
CHANGED
@@ -1,17 +1,23 @@
|
|
1
1
|
require "grab/version"
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module Grab
|
4
|
+
class ::Hash
|
5
|
+
alias_method :orig_values, :values
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def grab(*keys)
|
8
|
+
keys.map { |k| self.fetch(k) }
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
def values(*args)
|
12
|
+
if args.empty?
|
13
|
+
orig_values
|
14
|
+
else
|
15
|
+
args.map { |k| self[k] }
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Hash
|
22
|
+
include Grab
|
17
23
|
end
|
data/lib/grab/version.rb
CHANGED
data/spec/grab_spec.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'grab'
|
2
|
+
|
3
|
+
describe "#grab" do
|
4
|
+
let(:h) { {a: 1, b: 2} }
|
5
|
+
|
6
|
+
it "fetches a single value from the hash" do
|
7
|
+
h.grab(:a).should == [1]
|
8
|
+
end
|
9
|
+
|
10
|
+
it "fetches multiple values from the hash" do
|
11
|
+
h.grab(:a, :b).should == [1,2]
|
12
|
+
end
|
13
|
+
|
14
|
+
it "raises a KeyError for nonexistent keys" do
|
15
|
+
expect do
|
16
|
+
h.grab(:a, :b, :c)
|
17
|
+
end.to raise_error(KeyError, "key not found: :c")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#values" do
|
22
|
+
let(:h) { {a: 1, b: 2} }
|
23
|
+
|
24
|
+
it "retains the original behaviour of Hash#values" do
|
25
|
+
h.values.should == [1,2]
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns multiple values from the hash" do
|
29
|
+
h.values(:a, :b).should == [1,2]
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns nil for nonexistent keys" do
|
33
|
+
h.values(:a, :b, :c).should == [1,2,nil]
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grab
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,23 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-09-07 00:00:00.000000000 Z
|
13
|
-
dependencies:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
description: Fetch multiple keys from a Hash
|
15
31
|
email:
|
16
32
|
- github.aw@andywaite.com
|
@@ -26,6 +42,7 @@ files:
|
|
26
42
|
- grab.gemspec
|
27
43
|
- lib/grab.rb
|
28
44
|
- lib/grab/version.rb
|
45
|
+
- spec/grab_spec.rb
|
29
46
|
homepage: https://github.com/andyw8/grab
|
30
47
|
licenses: []
|
31
48
|
post_install_message:
|
@@ -50,4 +67,5 @@ rubygems_version: 1.8.24
|
|
50
67
|
signing_key:
|
51
68
|
specification_version: 3
|
52
69
|
summary: Fetch multiple keys from a Hash
|
53
|
-
test_files:
|
70
|
+
test_files:
|
71
|
+
- spec/grab_spec.rb
|