grab 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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.fetch(:foo)
7
- bar = params.fetch(:bar)
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 some of your params are optional:
16
+ or if want to use Array#fetch instead of Array#[]:
17
17
 
18
18
  def initialize(params)
19
- foo, bar = params.values(:foo, :bar)
19
+ foo, bar = params.grab!(:foo, :bar)
20
20
  end
21
21
 
22
22
  ## Installation
@@ -2,19 +2,14 @@ require "grab/version"
2
2
 
3
3
  module Grab
4
4
  def grab(*keys)
5
- keys.map { |k| self.fetch(k) }
5
+ keys.map { |k| self[k] }
6
6
  end
7
7
 
8
- def values(*args)
9
- args.map { |k| self[k] }
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
@@ -1,3 +1,3 @@
1
1
  module Grab
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -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 "#values" do
21
+ describe "#grab" do
22
22
  let(:h) { {a: 1, b: 2} }
23
23
 
24
- it "retains the original behaviour of Hash#values" do
25
- h.values.should == [1,2]
24
+ it "requests a single value from the hash" do
25
+ h.grab(:a).should == [1]
26
26
  end
27
27
 
28
- it "returns multiple values from the hash" do
29
- h.values(:a, :b).should == [1,2]
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.values(:a, :b, :c).should == [1,2,nil]
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
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-08 00:00:00.000000000 Z
12
+ date: 2012-09-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec