try_to 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b265e05b456bcddae7647ac5108450bd176c8973
4
- data.tar.gz: 1ffc1e94a179cb3aa60f91bdc91beaa5a5e43cc3
2
+ SHA256:
3
+ metadata.gz: a53f923148d5b7feaca7c296d23166c2a3a2ba747345844f84f420c540c40431
4
+ data.tar.gz: 163a6c9c3b7e5432749715da57053952ca6ac79fe60b2fe40186597d566dfd3d
5
5
  SHA512:
6
- metadata.gz: 02b58ac470c79cc489476f677362f2809679e723f434485a27bdc5936109502a0be7173789916241e6b3b5ddb8b0c25b51b3e0e47488430017c99c3f444a79bc
7
- data.tar.gz: 8d796018c77b4c7147ecae7d08be9dc4473b77cb7ef7849c004dbdbca5cc4a5f16067d970048b63178b09b4d6dbd302b822eb6f3370eecbeb8ad0d8c48563b66
6
+ metadata.gz: 41b63c7643991af44bfe7c44dc716d0b2d35954b827d38fca900243a9258fe93ffee331660ec53bfc69eda4bd266b7b614de7f80573cf25b6feb043384810d69
7
+ data.tar.gz: 332ecb1c745af1ff3b26eabf391d3546fae89e9997f0e820cb8a43b4b0c77135afb3e5c770283e9850334b8d1f745288a187b0babdb18aadf13c3f1f45df4440
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2012 Michael Kohl & Sergey Gopkalo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # try_to
2
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).
3
+ [![Build Status](https://travis-ci.org/citizen428/try_to.svg)](https://travis-ci.org/citizen428/methodfinder)
4
+ [![Gem Version](https://img.shields.io/gem/v/try_to.svg)](https://rubygems.org/gems/methodfinder)
5
+
6
+ This project started with a StackOverflow discussion between [Sergey Gopkalo](https://github.com/sevenmaxis/) and [Michael Kohl](https://github.com/citizen428),
7
+ which eventually lead to a prototype at [sevenmaxis/tryit](https://github.com/sevenmaxis/tryit).
8
+ `try_to` is an improved version based on the experience gained from that project,
9
+ but allows for much more sophisticated error handling.
10
+
11
+ ## Usage
4
12
 
5
13
  Instead of using Rails' `Object#try` like this,
6
14
 
@@ -10,38 +18,74 @@ you can do this:
10
18
 
11
19
  try_to { obj.met1.met2.met3.to_s }
12
20
 
13
- It's possible to customize which exceptions to handle:
21
+ ### Exceptions
22
+
23
+ It's possible to customize which exceptions to handle with `add_exception`:
14
24
 
15
- TryTo.exceptions << ZeroDivisionError
25
+ TryTo.add_exception(ZeroDivisionError)
26
+ #=> [NoMethodError, ZeroDivisionError]
16
27
  try_to { 1/0 } # will not raise an exception
17
28
 
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:
29
+ To remove an exception, use `remove_exception!`:
30
+
31
+ TryTo.exceptions
32
+ #=> [NoMethodError, ZeroDivisionError]
33
+ TryTo.remove_exception!(ZeroDivisionError)
34
+ #=> [NoMethodError]
35
+
36
+ You can also use `reset_exceptions!` to go back to only handle `NoMethodError`s.
19
37
 
20
- First off you can specify a handler with the call:
38
+ TryTo.exceptions
39
+ #=> [NoMethodError, RuntimeError, ZeroDivisionError]
40
+ TryTo.reset_exceptions!
41
+ #=> [NoMethodError]
42
+
43
+ ### Handlers
44
+
45
+ The default error handling strategy is to just return `nil`, but there are various
46
+ ways in which you can customize this behavior. All handlers can either be simple
47
+ values or an object responding to `#call`, which should take one argument, the
48
+ exception object.
49
+
50
+ Specifying a handler with the call for one time use:
21
51
 
22
52
  # use a handler function
23
- try_to(-> e {puts e.class}) { 1.foo } # prints "NoMethodError"
53
+ try_to(-> e { puts e.class }) { 1.foo }
54
+ # prints "NoMethodError"
24
55
  # or provide a simple value:
25
- try_to(42) { 1.foo } #=> 42
56
+ try_to(42) { 1.foo }
57
+ #=> 42
26
58
 
27
- Alternatively you can define specific handlers for different exception classes:
59
+ Registering handlers so they always are used:
28
60
 
29
- TryTo.handlers #=> {}
61
+ TryTo.handlers
62
+ #=> {}
30
63
  TryTo.add_handler(ZeroDivisionError, -> _ { puts "Ouch" })
31
- try_to { 1/0 } # prints "Ouch"
64
+ try_to { 1/0 }
65
+ # prints "Ouch"
32
66
  TryTo.add_handler(NoMethodError, -> _ { 23 })
33
- try_to { 1.foo } #=> 23
67
+ try_to { 1.foo }
68
+ #=> 23
34
69
  # or simply
35
70
  TryTo.add_handler(NoMethodError, 42)
36
71
  try_to { 1.foo } #=> 42
37
72
 
38
- Last but not least you can define a default handler for all the exceptions listed in `TryTo.exceptions`.
73
+ Removing handler:
74
+
75
+ TryTo.handlers
76
+ #=> {ZeroDivisionError=>#<Proc:0x0000000108921d60@(irb):1 (lambda)>}
77
+ TryTo.remove_handler!(ZeroDivisionError)
78
+ #=> {}
79
+
80
+ Last but not least you can define a default handler for all the exceptions
81
+ listed in `TryTo.exceptions`.
39
82
 
40
83
  TryTo.default_handler = 42
41
84
  try_to { 1.foo } #=> 42
42
85
  # or
43
86
  TryTo.default_handler = lambda { |_| puts "Something went wrong!" }
44
- try_to { 1.foo } # Outputs: Something went wrong!
87
+ try_to { 1.foo }
88
+ # Outputs: Something went wrong!
45
89
 
46
90
  Here's a complete example in the form of an IRB transcript:
47
91
 
@@ -91,12 +135,12 @@ Or install it yourself:
91
135
 
92
136
  ## License
93
137
 
94
- Lincesend under the MIT license. See the provided LICENSE file for details.
138
+ Licensed under the MIT license. See the provided LICENSE file for details.
95
139
 
96
140
  ## Contributing
97
141
 
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
142
+ 1. Fork it
143
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
144
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
145
+ 4. Push to the branch (`git push origin my-new-feature`)
146
+ 5. Create new Pull Request
data/lib/try_to.rb CHANGED
@@ -1,19 +1,37 @@
1
1
  module TryTo
2
+ @handlers = {}
3
+ @exceptions = [NoMethodError]
4
+
2
5
  class << self
3
6
  attr_accessor :default_handler
4
7
  attr_reader :exceptions, :handlers
5
- end
6
8
 
7
- def self.add_handler(exception, handler)
8
- @handlers.merge!(exception => handler)
9
- end
9
+ def add_handler(exception, handler)
10
+ @handlers[exception] = handler
11
+ @handlers
12
+ end
10
13
 
11
- @handlers = {}
12
- @exceptions = [NoMethodError]
14
+ def remove_handler!(exception)
15
+ @handlers.delete(exception)
16
+ @handlers
17
+ end
18
+
19
+ def add_exception(exception)
20
+ @exceptions << exception
21
+ end
22
+
23
+ def remove_exception!(exception)
24
+ @exceptions -= [exception]
25
+ end
26
+
27
+ def reset_exceptions!
28
+ @exceptions = [NoMethodError]
29
+ end
30
+ end
13
31
  end
14
32
 
15
33
  module Kernel
16
- def try_to(handler = nil)
34
+ private def try_to(handler = nil)
17
35
  yield if block_given?
18
36
  rescue *(TryTo.exceptions | TryTo.handlers.keys) => e
19
37
  handler = [handler,
@@ -21,6 +39,4 @@ module Kernel
21
39
  TryTo.default_handler].compact.first
22
40
  handler.respond_to?(:call) ? handler.call(e) : handler
23
41
  end
24
-
25
- private :try_to
26
42
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TryTo
4
+ VERSION = '1.2'.freeze
5
+ end
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: try_to
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: '1.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Kohl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-17 00:00:00.000000000 Z
11
+ date: 2018-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rake
14
+ name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1.16'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '1.16'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: 2.9.0
47
+ version: '10.0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: 2.9.0
54
+ version: '10.0'
41
55
  description: Try methods without exceptions
42
56
  email:
43
57
  - citizen428@gmail.com
@@ -45,19 +59,16 @@ executables: []
45
59
  extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
- - ".gitignore"
49
- - ".rspec"
50
- - Gemfile
51
- - LICENSE
62
+ - LICENSE.txt
52
63
  - README.md
53
- - Rakefile
54
64
  - lib/try_to.rb
55
- - spec/spec_helper.rb
56
- - spec/try_to_spec.rb
57
- - try_to.gemspec
65
+ - lib/try_to/version.rb
58
66
  homepage: https://github.com/citizen428/try_to
59
- licenses: []
60
- metadata: {}
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ bug_tracker_uri: https://github.com/citizen428/try_to/issues
71
+ source_code_uri: https://github.com/citizen428/try_to
61
72
  post_install_message:
62
73
  rdoc_options: []
63
74
  require_paths:
@@ -74,11 +85,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
85
  version: '0'
75
86
  requirements: []
76
87
  rubyforge_project:
77
- rubygems_version: 2.4.5
88
+ rubygems_version: 2.7.6
78
89
  signing_key:
79
90
  specification_version: 4
80
91
  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:
92
+ test_files: []
data/.gitignore DELETED
@@ -1,18 +0,0 @@
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
18
- /*.gemspec~
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --color
2
- --format progress
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in try_to.gemspec
4
- gemspec
data/LICENSE DELETED
@@ -1,22 +0,0 @@
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/Rakefile DELETED
@@ -1,37 +0,0 @@
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/spec/spec_helper.rb DELETED
@@ -1,14 +0,0 @@
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 DELETED
@@ -1,76 +0,0 @@
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 DELETED
@@ -1,18 +0,0 @@
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.1'
15
-
16
- gem.add_development_dependency 'rake'
17
- gem.add_development_dependency 'rspec', '~>2.9.0'
18
- end