grab 0.0.4 → 0.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.
- data/README.md +4 -4
- data/lib/grab.rb +3 -8
- data/lib/grab/version.rb +1 -1
- data/spec/grab_spec.rb +10 -10
- metadata +2 -2
data/README.md
CHANGED
@@ -3,8 +3,8 @@
|
|
3
3
|
Grab provides an clean way to fetch multiple values from a hash. Instead of:
|
4
4
|
|
5
5
|
def initialize(params)
|
6
|
-
foo = params
|
7
|
-
bar = params
|
6
|
+
foo = params[:foo]
|
7
|
+
bar = params[:bar]
|
8
8
|
end
|
9
9
|
|
10
10
|
you can write:
|
@@ -13,10 +13,10 @@ you can write:
|
|
13
13
|
foo, bar = params.grab(:foo, :bar)
|
14
14
|
end
|
15
15
|
|
16
|
-
or if
|
16
|
+
or if want to use Array#fetch instead of Array#[]:
|
17
17
|
|
18
18
|
def initialize(params)
|
19
|
-
foo, bar = params.
|
19
|
+
foo, bar = params.grab!(:foo, :bar)
|
20
20
|
end
|
21
21
|
|
22
22
|
## Installation
|
data/lib/grab.rb
CHANGED
@@ -2,19 +2,14 @@ require "grab/version"
|
|
2
2
|
|
3
3
|
module Grab
|
4
4
|
def grab(*keys)
|
5
|
-
keys.map { |k| self
|
5
|
+
keys.map { |k| self[k] }
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
9
|
-
|
8
|
+
def grab!(*keys)
|
9
|
+
keys.map { |k| self.fetch(k) }
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
class Hash
|
14
14
|
include Grab
|
15
|
-
|
16
|
-
def values(*args)
|
17
|
-
args = keys if args.empty?
|
18
|
-
super
|
19
|
-
end
|
20
15
|
end
|
data/lib/grab/version.rb
CHANGED
data/spec/grab_spec.rb
CHANGED
@@ -1,35 +1,35 @@
|
|
1
1
|
require 'grab'
|
2
2
|
|
3
|
-
describe "#grab" do
|
3
|
+
describe "#grab!" do
|
4
4
|
let(:h) { {a: 1, b: 2} }
|
5
5
|
|
6
6
|
it "fetches a single value from the hash" do
|
7
|
-
h.grab(:a).should == [1]
|
7
|
+
h.grab!(:a).should == [1]
|
8
8
|
end
|
9
9
|
|
10
10
|
it "fetches multiple values from the hash" do
|
11
|
-
h.grab(:a, :b).should == [1,2]
|
11
|
+
h.grab!(:a, :b).should == [1,2]
|
12
12
|
end
|
13
13
|
|
14
14
|
it "raises a KeyError for nonexistent keys" do
|
15
15
|
expect do
|
16
|
-
h.grab(:a, :b, :c)
|
16
|
+
h.grab!(:a, :b, :c)
|
17
17
|
end.to raise_error(KeyError, "key not found: :c")
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
describe "#
|
21
|
+
describe "#grab" do
|
22
22
|
let(:h) { {a: 1, b: 2} }
|
23
23
|
|
24
|
-
it "
|
25
|
-
h.
|
24
|
+
it "requests a single value from the hash" do
|
25
|
+
h.grab(:a).should == [1]
|
26
26
|
end
|
27
27
|
|
28
|
-
it "
|
29
|
-
h.
|
28
|
+
it "requests multiple values from the hash" do
|
29
|
+
h.grab(:a, :b).should == [1,2]
|
30
30
|
end
|
31
31
|
|
32
32
|
it "returns nil for nonexistent keys" do
|
33
|
-
h.
|
33
|
+
h.grab(:a, :b, :c).should == [1,2,nil]
|
34
34
|
end
|
35
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.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|