exception_utilities 1.0.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.
@@ -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,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in exception_utilities.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Kenta Murata
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,69 @@
1
+ # ExceptionUtilities
2
+
3
+ Utilities for handling exceptions.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'exception_utilities'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install exception_utilities
18
+
19
+ ## Usage
20
+
21
+ ### Ignoring specific exceptions
22
+
23
+ Use ```exceptions_ignoring_eval```:
24
+
25
+ ```ruby
26
+ require 'exception_utilities/kernel'
27
+
28
+ exceptions_ignoring_eval(LoadError) do
29
+ require 'foo'
30
+ end
31
+ ```
32
+
33
+ ### Rescue exceptions having specific message
34
+
35
+ Use ```exceptions_with_message```:
36
+
37
+ ```ruby
38
+ require 'exception_utilities/kernel'
39
+
40
+ begin
41
+ SomeModel.create!
42
+ rescue exceptions_with_message(/\Bfoo_bar_id\B/, ActiveRecord::RecordNotFound)
43
+ Rails.logger.debug([$!.message, *$!.backtrace].join("\n"))
44
+ end
45
+ ```
46
+
47
+ ### Creating exception matcher
48
+
49
+ Use ```exception_matcher```:
50
+
51
+ ```ruby
52
+ require 'exception_utilities/kernel'
53
+
54
+ begin
55
+ # some routine
56
+ rescue exception_matcher {|exc| exc.count <=1 }
57
+ # ignore
58
+ rescue exception_matcher {|exc| exc.count > 1 }
59
+ raise
60
+ end
61
+ ```
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/exception_utilities/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Kenta Murata"]
6
+ gem.email = ["mrkn@mrkn.jp"]
7
+ gem.description = %q{Utilities for handling exceptions}
8
+ gem.summary = %q{Exception handling utilities}
9
+ gem.homepage = "http://github.com/mrkn/exception_utilities"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "exception_utilities"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ExceptionUtilities::VERSION
17
+
18
+ gem.add_development_dependency('rspec', '~> 2.12.0')
19
+ if RUBY_VERSION < '1.9'
20
+ gem.add_development_dependency('rcov', '~> 1.0.0')
21
+ else
22
+ gem.add_development_dependency('simplecov-rcov', '~> 0.2.3')
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ require "exception_utilities/version"
2
+
3
+ module ExceptionUtilities
4
+ class Error < StandardError; end
5
+
6
+ module_function
7
+
8
+ def exceptions_ignoring_eval(*modules)
9
+ unless modules.all? {|ec| Module === ec }
10
+ raise Error, 'only Module instances are accepted'
11
+ end
12
+ begin
13
+ yield
14
+ rescue *modules => exception
15
+ exception
16
+ end
17
+ end
18
+
19
+ def exception_matcher(&block)
20
+ Module.new do
21
+ (class << self; self; end).class_eval do
22
+ define_method(:===, &block)
23
+ end
24
+ end
25
+ end
26
+
27
+ def exceptions_with_message(pattern, *exception_classes)
28
+ exception_classes << StandardError if exception_classes.empty?
29
+ exception_matcher do |exception|
30
+ case exception
31
+ when *exception_classes
32
+ pattern === exception.message rescue false
33
+ else
34
+ false
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module ExceptionUtilities
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,125 @@
1
+ require 'spec_helper'
2
+
3
+ describe ExceptionUtilities do
4
+ describe '.exceptions_ignoring_eval' do
5
+ context 'when called with an exception class' do
6
+ it 'should ignore an exception which is an instance of specified exception class' do
7
+ expect {
8
+ described_class.exceptions_ignoring_eval(RuntimeError) { raise }
9
+ }.to_not raise_error(RuntimeError)
10
+ end
11
+
12
+ it 'should return the ignored exception' do
13
+ described_class.exceptions_ignoring_eval(RuntimeError) { raise }.should be_a(RuntimeError)
14
+
15
+ exc = Class.new(RuntimeError)
16
+ described_class.exceptions_ignoring_eval(RuntimeError) { raise exc }.should be_a(exc)
17
+ end
18
+
19
+ it 'should ignore an exception which is an instance_of an exception class that is a subclass of the specified one' do
20
+ exc = Class.new(RuntimeError)
21
+ expect {
22
+ described_class.exceptions_ignoring_eval(RuntimeError) { raise exc }
23
+ }.to_not raise_error(exc)
24
+ end
25
+
26
+ it 'should not ignore an exception which is an instance of an exception class that is not a subclass of the specified one' do
27
+ exc = Class.new(TypeError)
28
+ expect {
29
+ described_class.exceptions_ignoring_eval(RuntimeError) { raise exc }
30
+ }.to raise_error(exc)
31
+ end
32
+ end
33
+
34
+ context 'when called with two exception classes' do
35
+ it 'should ignore an exception which is an instance of an exception class that is a subclass of each of these' do
36
+ expect {
37
+ described_class.exceptions_ignoring_eval(LoadError, RuntimeError) { raise LoadError }
38
+ }.to_not raise_error(LoadError)
39
+
40
+ exc = Class.new(RuntimeError)
41
+ expect {
42
+ described_class.exceptions_ignoring_eval(LoadError, RuntimeError) { raise exc }
43
+ }.to_not raise_error(RuntimeError)
44
+ end
45
+ end
46
+
47
+ context 'when called with an object not an exception class' do
48
+ it 'should raise ExceptionUtilities::Error' do
49
+ expect {
50
+ described_class.exceptions_ignoring_eval(Object.new) { }
51
+ }.to raise_error(ExceptionUtilities::Error)
52
+ end
53
+ end
54
+
55
+ context 'when called with an exception class and an object not an exception class' do
56
+ it 'should raise ExceptionUtilities::Error' do
57
+ expect {
58
+ described_class.exceptions_ignoring_eval(LoadError, Object.new) { }
59
+ }.to raise_error(ExceptionUtilities::Error)
60
+ end
61
+ end
62
+ end
63
+
64
+ describe '#exception_matcher' do
65
+ context 'when called with a block' do
66
+ context 'when subject.=== is called with an object' do
67
+ it 'should call the block with the object' do
68
+ obj = double('object')
69
+ obj.should_receive(:confirm).and_return(:block_called)
70
+ (described_class.exception_matcher {|x| x.confirm } === obj).should == :block_called
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ describe '#exceptions_with_message' do
77
+ context 'when called with "foo bar"' do
78
+ subject { described_class.exceptions_with_message('foo bar') }
79
+
80
+ it { should_not === Exception.new('foo bar') }
81
+
82
+ it { should === StandardError.new('foo bar') }
83
+
84
+ it { should === RuntimeError.new('foo bar') }
85
+
86
+ it { should_not === 'foo bar' }
87
+ end
88
+
89
+ context 'when called with "foo bar", RuntimeError, and TypeError' do
90
+ subject { described_class.exceptions_with_message('foo bar', RuntimeError, TypeError) }
91
+
92
+ it { should_not === Exception.new('foo bar') }
93
+
94
+ it { should === RuntimeError.new('foo bar') }
95
+
96
+ it { should === TypeError.new('foo bar') }
97
+
98
+ it { should_not === 'foo bar' }
99
+ end
100
+
101
+ context 'when called with /foo bar/' do
102
+ subject { described_class.exceptions_with_message(/foo bar/) }
103
+
104
+ it { should_not === Exception.new('abc foo bar xyz') }
105
+
106
+ it { should === StandardError.new('abc foo bar xyz') }
107
+
108
+ it { should === RuntimeError.new('abc foo bar xyz') }
109
+
110
+ it { should_not === 'abc foo bar xyz' }
111
+ end
112
+
113
+ context 'when called with /foo bar/, RuntimeError, and TypeError' do
114
+ subject { described_class.exceptions_with_message(/foo bar/, RuntimeError, TypeError) }
115
+
116
+ it { should_not === Exception.new('abc foo bar xyz') }
117
+
118
+ it { should === RuntimeError.new('abc foo bar xyz') }
119
+
120
+ it { should === TypeError.new('abc foo bar xyz') }
121
+
122
+ it { should_not === 'abc foo bar xyz' }
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,19 @@
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"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
18
+
19
+ require 'exception_utilities'
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exception_utilities
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Kenta Murata
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-12-05 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 63
29
+ segments:
30
+ - 2
31
+ - 12
32
+ - 0
33
+ version: 2.12.0
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rcov
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 1
47
+ - 0
48
+ - 0
49
+ version: 1.0.0
50
+ type: :development
51
+ version_requirements: *id002
52
+ description: Utilities for handling exceptions
53
+ email:
54
+ - mrkn@mrkn.jp
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files: []
60
+
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - exception_utilities.gemspec
69
+ - lib/exception_utilities.rb
70
+ - lib/exception_utilities/version.rb
71
+ - spec/exception_utilities_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: http://github.com/mrkn/exception_utilities
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options: []
78
+
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.24
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Exception handling utilities
106
+ test_files:
107
+ - spec/exception_utilities_spec.rb
108
+ - spec/spec_helper.rb