grab 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new('spec')
6
+
7
+ # If you want to make this the default task
8
+ task :default => :spec
@@ -14,4 +14,5 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "grab"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Grab::VERSION
17
+ gem.add_development_dependency "rspec"
17
18
  end
@@ -1,17 +1,23 @@
1
1
  require "grab/version"
2
2
 
3
- class Hash
4
- alias_method :old_values, :values
3
+ module Grab
4
+ class ::Hash
5
+ alias_method :orig_values, :values
5
6
 
6
- def grab(*keys)
7
- keys.map { |k| self.fetch(k) }
8
- end
7
+ def grab(*keys)
8
+ keys.map { |k| self.fetch(k) }
9
+ end
9
10
 
10
- def values(*args)
11
- if args.empty?
12
- old_values
13
- else
14
- args.map { |k| self[k] }
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
@@ -1,3 +1,3 @@
1
1
  module Grab
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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.2
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