try_to 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.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +102 -0
- data/Rakefile +37 -0
- data/lib/try_to.rb +28 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/try_to_spec.rb +76 -0
- data/try_to.gemspec +18 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 97985eb519dbff474a97eec6322beed1007b6de0
|
4
|
+
data.tar.gz: a86fadb1eab2dae7e6db0192faf3a7a21e27f82f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c7cba069537e556aca316a11969cd7d566ec037e7025a65ab2a513ee487c10620ba7376ad1f52d6dac69fa2637b31145fe79b052377ceceed62acace871f4c6d
|
7
|
+
data.tar.gz: 3cd82c83dc4ece5b8efbc92f33908ef978f5f8e80802d7363f9b589a3135d887bee37f7ee7da3c3b1570e6e3fa0bc1690868aeca9bd58eb4e7f2fa7ba4d623c6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Michael Kohl & Sergey Gopkalo
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# try_to
|
2
|
+
|
3
|
+
This project started with a StackOverflow discussion between [Sergey Gopkalo](https://github.com/sevenmaxis/) and [Michael Kohl](https://github.com/citizen428), which eventually lead to a prototype at [sevenmaxis/tryit](https://github.com/sevenmaxis/tryit). `try_to` is an improved version based on the experience gained from that project, but allows for much more sophisticated error handling (in less than 30 lines of Ruby).
|
4
|
+
|
5
|
+
Instead of using Rails' `Object#try` like this,
|
6
|
+
|
7
|
+
obj1.try(:met1).try(:met2).try(:met3).to_s
|
8
|
+
|
9
|
+
you can do this:
|
10
|
+
|
11
|
+
try_to { obj.met1.met2.met3.to_s }
|
12
|
+
|
13
|
+
It's possible to customize which exceptions to handle:
|
14
|
+
|
15
|
+
TryTo.exceptions << ZeroDivisionError
|
16
|
+
try_to { 1/0 } # will not raise an exception
|
17
|
+
|
18
|
+
The default error handling strategy is to just return `nil`, but there are various ways you can customize this behavior. All handlers can either be simple values or an object responding to `#call`, which should take one argument, the exception object:
|
19
|
+
|
20
|
+
First off you can specify a handler with the call:
|
21
|
+
|
22
|
+
# use a handler function
|
23
|
+
try_to(-> e {puts e.class}) { 1.foo } # prints "NoMethodError"
|
24
|
+
# or provide a simple value:
|
25
|
+
try_to(42) { 1.foo } #=> 42
|
26
|
+
|
27
|
+
Alternatively you can define specific handlers for different exception classes:
|
28
|
+
|
29
|
+
TryTo.handlers #=> {}
|
30
|
+
TryTo.add_handler(ZeroDivisionError, -> _ { puts "Ouch" })
|
31
|
+
try_to { 1/0 } # prints "Ouch"
|
32
|
+
TryTo.add_handler(NoMethodError, -> _ { 23 })
|
33
|
+
try_to { 1.foo } #=> 23
|
34
|
+
# or simply
|
35
|
+
TryTo.add_handler(NoMethodError, 42)
|
36
|
+
try_to { 1.foo } #=> 42
|
37
|
+
|
38
|
+
Last but not least you can define a default handler for all the exceptions listed in `TryTo.exceptions`.
|
39
|
+
|
40
|
+
TryTo.default_handler = 42
|
41
|
+
try_to { 1.foo } #=> 42
|
42
|
+
# or
|
43
|
+
TryTo.default_handler = lambda { |_| puts "Something went wrong!" }
|
44
|
+
try_to { 1.foo } # Outputs: Something went wrong!
|
45
|
+
|
46
|
+
Here's a complete example in the form of an IRB transcript:
|
47
|
+
|
48
|
+
# default behavior
|
49
|
+
try_to #=> nil
|
50
|
+
try_to {} #=> nil
|
51
|
+
class Foo ; end
|
52
|
+
try_to { Foo.new.foo } #=> nil
|
53
|
+
|
54
|
+
# this will raise an exception
|
55
|
+
try_to { 1 / 0 }
|
56
|
+
ZeroDivisionError: divided by 0
|
57
|
+
# let's fix that
|
58
|
+
TryTo.exceptions << ZeroDivisionError #=> [NoMethodError, ZeroDivisionError]
|
59
|
+
try_to { 1 / 0 } #=> nil
|
60
|
+
|
61
|
+
# change the default handler
|
62
|
+
TryTo.default_handler = -> e { puts e.class }
|
63
|
+
try_to { 1 / 0 } # prints "ZeroDivisionError"
|
64
|
+
try_to { Foo.new.foo } # prints "ZeroDivisionError"
|
65
|
+
|
66
|
+
# new behavior for ZeroDivisionError
|
67
|
+
TryTo.add_handler(ZeroDivisionError, -> _ { puts "You shouldn't divide by 0!"})
|
68
|
+
try_to { 1 / 0 } # prints: "You shouldn't divide by 0!"
|
69
|
+
try_to { Foo.new.foo} # still prints "NoMethodError"
|
70
|
+
|
71
|
+
# change handler at call site
|
72
|
+
try_to(-> _ {puts "Ouch!"}) { Foo.new.foo } # prints "Ouch!"
|
73
|
+
|
74
|
+
## Installation
|
75
|
+
|
76
|
+
Add this line to your application's Gemfile:
|
77
|
+
|
78
|
+
gem 'try_to'
|
79
|
+
|
80
|
+
And then execute:
|
81
|
+
|
82
|
+
$ bundle
|
83
|
+
|
84
|
+
Or install it yourself:
|
85
|
+
|
86
|
+
$ gem install try_to
|
87
|
+
|
88
|
+
## Authors
|
89
|
+
|
90
|
+
[Michael Kohl](https://github.com/citizen428). There's some leftover code (primarily in the specs) from [sevenmaxis/tryit](https://github.com/sevenmaxis/tryit) by [Sergey Gopkalo](https://github.com/sevenmaxis/).
|
91
|
+
|
92
|
+
## License
|
93
|
+
|
94
|
+
Lincesend under the MIT license. See the provided LICENSE file for details.
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
1. Fork it
|
99
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
100
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
101
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
102
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rspec/core/rake_task'
|
2
|
+
require 'fileutils'
|
3
|
+
GEMSPEC = 'try_to.gemspec'
|
4
|
+
|
5
|
+
task :default => :spec
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new do |t|
|
8
|
+
t.rspec_opts = '--format documentation'
|
9
|
+
end
|
10
|
+
|
11
|
+
def gemspec
|
12
|
+
@gemspec ||= eval(File.read(GEMSPEC), binding, GEMSPEC)
|
13
|
+
end
|
14
|
+
|
15
|
+
namespace :gem do
|
16
|
+
desc "Build the gem"
|
17
|
+
task :build => :generate_gemspec do
|
18
|
+
sh "gem build #{GEMSPEC}"
|
19
|
+
FileUtils.mkdir_p 'pkg'
|
20
|
+
FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Install the gem locally (without docs)"
|
24
|
+
task :install => :build do
|
25
|
+
sh %{gem install pkg/#{gemspec.name}-#{gemspec.version} --no-rdoc --no-ri}
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Generate the gemspec"
|
29
|
+
task :generate_gemspec do
|
30
|
+
puts gemspec.to_ruby
|
31
|
+
end
|
32
|
+
|
33
|
+
desc "Validate the gemspec"
|
34
|
+
task :validate_gemspec do
|
35
|
+
gemspec.validate
|
36
|
+
end
|
37
|
+
end
|
data/lib/try_to.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module TryTo
|
2
|
+
class << self
|
3
|
+
attr_accessor :exceptions, :default_handler, :handlers
|
4
|
+
private :handlers=
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.add_handler(exception, handler)
|
8
|
+
self.handlers.merge!(exception => handler)
|
9
|
+
end
|
10
|
+
|
11
|
+
self.handlers = {}
|
12
|
+
self.exceptions = [NoMethodError]
|
13
|
+
end
|
14
|
+
|
15
|
+
module Kernel
|
16
|
+
def try_to(handler = nil, &block)
|
17
|
+
block.call if block
|
18
|
+
rescue *(TryTo.exceptions | TryTo.handlers.keys) => e
|
19
|
+
handler = [
|
20
|
+
handler,
|
21
|
+
TryTo.handlers[e.class],
|
22
|
+
TryTo.default_handler
|
23
|
+
].compact.first
|
24
|
+
handler.respond_to?(:call) ? handler.call(e) : handler
|
25
|
+
end
|
26
|
+
|
27
|
+
private :try_to
|
28
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require_relative "../lib/try_to"
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
end
|
data/spec/try_to_spec.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Kernel do
|
4
|
+
describe "#try_to" do
|
5
|
+
let(:obj) { Object.new }
|
6
|
+
|
7
|
+
context "default behavior" do
|
8
|
+
it "returns nil when no block is provided" do
|
9
|
+
try_to.should be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns nil when an empty block is provided" do
|
13
|
+
try_to{}.should be_nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it "doesn't catch unspecified exceptions" do
|
17
|
+
class A; def foo; raise "test exception" end end
|
18
|
+
expect do
|
19
|
+
try_to { A.new.foo }
|
20
|
+
end.to raise_error(RuntimeError, "test exception")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "handles specified exceptions by returning nil" do
|
24
|
+
result = nil
|
25
|
+
expect{ result = try_to{ obj.foo } }.to_not raise_error(NoMethodError)
|
26
|
+
result.should be_nil
|
27
|
+
end
|
28
|
+
|
29
|
+
it "handles chained calls" do
|
30
|
+
def obj.foo; self end
|
31
|
+
def obj.boo; "boo" end
|
32
|
+
try_to{ obj.foo.boo }.should == 'boo'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "doesn't raise exceptions in chain calls" do
|
36
|
+
def obj.foo; self end
|
37
|
+
def obj.koo; self end
|
38
|
+
expect{ try_to{ obj.foo.koo.too }}.to_not raise_error(NoMethodError)
|
39
|
+
expect{ try_to{ obj.foo.too.koo }}.to_not raise_error(NoMethodError)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "can add exceptions at runtime" do
|
43
|
+
expect do
|
44
|
+
try_to { 1/0 }
|
45
|
+
end.to raise_error(ZeroDivisionError)
|
46
|
+
|
47
|
+
TryTo.exceptions << ZeroDivisionError
|
48
|
+
|
49
|
+
expect do
|
50
|
+
try_to { 1/0 }
|
51
|
+
end.to_not raise_error(ZeroDivisionError)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
context "handlers" do
|
57
|
+
it "uses a default handler" do
|
58
|
+
TryTo.default_handler = -> e { e.class }
|
59
|
+
try_to{obj.foo}.should == NoMethodError
|
60
|
+
end
|
61
|
+
|
62
|
+
it "uses exception class specific handlers" do
|
63
|
+
TryTo.handlers[TypeError] = lambda { |e| puts e }
|
64
|
+
$stdout.should_receive(:puts)
|
65
|
+
try_to { raise TypeError }
|
66
|
+
end
|
67
|
+
|
68
|
+
it "can specify a handler on the fly" do
|
69
|
+
try_to(42) do
|
70
|
+
obj.foo
|
71
|
+
end.should == 42
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
data/try_to.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
Gem::Specification.new do |gem|
|
3
|
+
gem.authors = ['Michael Kohl']
|
4
|
+
gem.email = ['citizen428@gmail.com']
|
5
|
+
gem.description = %q{Try methods without exceptions}
|
6
|
+
gem.summary = %q{An alternative approach to Rails' Object#try}
|
7
|
+
gem.homepage = 'https://github.com/citizen428/try_to'
|
8
|
+
|
9
|
+
gem.files = `git ls-files`.split($\)
|
10
|
+
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
11
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
12
|
+
gem.name = 'try_to'
|
13
|
+
gem.require_paths = ['lib']
|
14
|
+
gem.version = '1.0'
|
15
|
+
|
16
|
+
gem.add_development_dependency 'rake'
|
17
|
+
gem.add_development_dependency 'rspec', '~>2.9.0'
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: try_to
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Kohl
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.9.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.9.0
|
41
|
+
description: Try methods without exceptions
|
42
|
+
email:
|
43
|
+
- citizen428@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .rspec
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/try_to.rb
|
55
|
+
- spec/spec_helper.rb
|
56
|
+
- spec/try_to_spec.rb
|
57
|
+
- try_to.gemspec
|
58
|
+
homepage: https://github.com/citizen428/try_to
|
59
|
+
licenses: []
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.0.3
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: An alternative approach to Rails' Object#try
|
81
|
+
test_files:
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
- spec/try_to_spec.rb
|
84
|
+
has_rdoc:
|