verbose_hash_fetch 0.0.1

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --order random
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in verbose_hash_fetch.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 iain
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,92 @@
1
+ # VerboseHashFetch
2
+
3
+ This gem monkey patches `Hash#fetch` so that KeyErrors show the contents of the
4
+ hash in which the key wasn't found.
5
+
6
+ ## Example
7
+
8
+ We are taught to use `Hash#fetch` instead of `Hash#[]`, so that we don't get
9
+ unexpected `nil`s floating around in our code. `Hash#fetch` will raise an
10
+ exception when the key isn't found, right where you tried to access it. Rather
11
+ fail early then have a nil pop up somewhere else!
12
+
13
+ But there is one thing that annoys me. Say you're writing this code:
14
+
15
+ ``` ruby
16
+ def hello(options)
17
+ name = options.fetch(:name)
18
+ puts "Hello, #{name}!"
19
+ end
20
+ ```
21
+
22
+ Now, you're calling this method, passing in some params that you got from
23
+ somewhere else:
24
+
25
+ ``` ruby
26
+ hello(params)
27
+ # => key not found: :name (KeyError)
28
+ ```
29
+
30
+ Blast! Now I have to see why `params` doesn't have the key `:name` in it. Did I
31
+ misspel it somewhere? Was the name parameter a String instead of a Symbol?
32
+
33
+ What shall I do: put a `puts` in the code? Should I `raise params.inspect`,
34
+ should I launch a debugger, or should I stare at the code long enough to
35
+ see the mistake?
36
+
37
+ All these options suck. Just have `verbose_hash_fetch` loaded, and you'll
38
+ see the contents of the hash appear in the error itself!
39
+
40
+ ``` ruby
41
+ require 'verbose_hash_fetch'
42
+
43
+ hello(params)
44
+ # => key not found: :name in Hash: {:naem=>"Avdi"} (KeyError)
45
+ ```
46
+
47
+ No more guessing, no more retrying. See it when it happens!
48
+
49
+ ## Installation
50
+
51
+ Add this line to your application's Gemfile:
52
+
53
+ ``` ruby
54
+ gem 'verbose_hash_fetch'
55
+ ```
56
+
57
+ And then execute:
58
+
59
+ ```
60
+ $ bundle
61
+ ```
62
+
63
+ Or install it yourself as:
64
+
65
+ ```
66
+ $ gem install verbose_hash_fetch
67
+ ```
68
+
69
+ ## Usage
70
+
71
+ If you have `verbose_hash_fetch` in your Gemfile, and you're using Rails, then
72
+ you don't need to do anything.
73
+
74
+ If you need to load it manually:
75
+
76
+ ``` ruby
77
+ require 'verbose_hash_fetch'
78
+ ```
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create new Pull Request
87
+
88
+
89
+ ## Credits
90
+
91
+ I was complaining on Twitter, so [Roel van Dijk](http://twitter.com/rdvdijk)
92
+ made it and put it in a Gist. I just converted it into a gem.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ class KeyError
2
+ attr_accessor :hash
3
+
4
+ def to_s
5
+ if hash
6
+ "#{super} in #{hash.class}: #{hash.inspect}"
7
+ else
8
+ super
9
+ end
10
+ end
11
+ end
12
+
13
+ class Hash
14
+
15
+ alias_method :_fetch, :fetch
16
+
17
+ def fetch(*args, &block)
18
+ _fetch *args, &block
19
+ rescue KeyError => key_error
20
+ key_error.hash = self
21
+ raise
22
+ end
23
+
24
+ end
@@ -0,0 +1,47 @@
1
+ require 'verbose_hash_fetch'
2
+
3
+ describe "VerboseHashFetch" do
4
+
5
+ let(:hash) { { :foo => "bar" } }
6
+
7
+ context "with one argument" do
8
+
9
+ it "fetches an existing key" do
10
+ hash.fetch(:foo).should eq "bar"
11
+ end
12
+
13
+ it "raises a KeyError for non existing keys" do
14
+ expect { hash.fetch(:bar) }.to raise_error(KeyError)
15
+ end
16
+
17
+ it "raises a KeyError with self.inspect in the message" do
18
+ expect { hash.fetch(:bar) }.to raise_error("key not found: :bar in Hash: {:foo=>\"bar\"}")
19
+ end
20
+
21
+ end
22
+
23
+ context "with two arguments" do
24
+
25
+ it "fetches the existing value" do
26
+ hash.fetch(:foo, "default").should eq "bar"
27
+ end
28
+
29
+ it "uses the 2nd argument when key not found" do
30
+ hash.fetch(:bar, "default").should eq "default"
31
+ end
32
+
33
+ end
34
+
35
+ context "with a block" do
36
+
37
+ it "fetches the existing value" do
38
+ hash.fetch(:foo) { "default" }.should eq "bar"
39
+ end
40
+
41
+ it "evaluates the block when key not found" do
42
+ hash.fetch(:bar) { |k| "default #{k}" }.should eq "default bar"
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "verbose_hash_fetch"
7
+ gem.version = '0.0.1'
8
+ gem.authors = ["iain"]
9
+ gem.email = ["iain@iain.nl"]
10
+ gem.description = %q{Monkey patches Hash#fetch to also show the entire hash in the error}
11
+ gem.summary = %q{Monkey patches Hash#fetch to also show the entire hash in the error}
12
+ gem.homepage = "https://github.com/iain/verbose_hash_fetch"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_development_dependency "rspec"
20
+ gem.add_development_dependency "rake"
21
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: verbose_hash_fetch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - iain
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-03 00:00:00.000000000 Z
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'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Monkey patches Hash#fetch to also show the entire hash in the error
47
+ email:
48
+ - iain@iain.nl
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/verbose_hash_fetch.rb
60
+ - spec/verbose_hash_fetch_spec.rb
61
+ - verbose_hash_fetch.gemspec
62
+ homepage: https://github.com/iain/verbose_hash_fetch
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.23
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Monkey patches Hash#fetch to also show the entire hash in the error
86
+ test_files:
87
+ - spec/verbose_hash_fetch_spec.rb