matching_exceptions 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +90 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/matching_exceptions.rb +22 -0
- data/lib/matching_exceptions/version.rb +3 -0
- data/matching_exceptions.gemspec +27 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4f4ab9f4cf43a72871a42df60502948d90870e28
|
4
|
+
data.tar.gz: 0b0f878e6fdcd6cc05cbabe81ff5a0018a029326
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cbc497a48a3d0607bffdbc80eea3b480a11d575b9f869aabee206a8dc4198c8c70adf9a14f4e8f33e410b9be355784b810ac4ec7b0fdf26b88f3930622c15415
|
7
|
+
data.tar.gz: c4f3c8f55ed37c29986afe92067b9bc1960d30d6f3172ab7a9a66ce53b594e527ecd847ae6e709a9f2e95073fa92ac94df9097b68bc2e29c421739fe733b936f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Guilherme Carvalho
|
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
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# MatchingExceptions
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/guava/matching_exceptions.svg?branch=master)](https://travis-ci.org/guava/matching_exceptions)
|
4
|
+
|
5
|
+
Rescues exceptions using pattern matching.
|
6
|
+
|
7
|
+
Usage example:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
def foo
|
11
|
+
raise StandardError.new('foo bar')
|
12
|
+
rescue ME.matches(/bar/)
|
13
|
+
puts 'it works!'
|
14
|
+
end
|
15
|
+
|
16
|
+
pry(main)> foo
|
17
|
+
# it works!
|
18
|
+
```
|
19
|
+
|
20
|
+
An ideal use case for this gem:
|
21
|
+
|
22
|
+
**Old code**
|
23
|
+
```ruby
|
24
|
+
def bar
|
25
|
+
do_something_complicated
|
26
|
+
rescue StandardError => e
|
27
|
+
if e.message =~ /some kind of error/
|
28
|
+
treat_error
|
29
|
+
else
|
30
|
+
treat_rest
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
**New code**
|
36
|
+
```ruby
|
37
|
+
def bar
|
38
|
+
do_something_complicated
|
39
|
+
rescue ME.matches(/some kind of error/)
|
40
|
+
treat_error
|
41
|
+
rescue StandardError
|
42
|
+
treat_rest
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
## Installation
|
47
|
+
|
48
|
+
Add this line to your application's Gemfile:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
gem 'matching_exceptions'
|
52
|
+
```
|
53
|
+
|
54
|
+
And then execute:
|
55
|
+
|
56
|
+
$ bundle
|
57
|
+
|
58
|
+
Or install it yourself as:
|
59
|
+
|
60
|
+
$ gem install matching_exceptions
|
61
|
+
|
62
|
+
## Usage
|
63
|
+
|
64
|
+
Call `ME.matches(something)` in a rescue block to rescue exceptions with `something`.
|
65
|
+
|
66
|
+
By default, `message` will be matched (i.e. the exception will be rescued if `e.message == something`, or `e.message =~ something` if `something` is a regex).
|
67
|
+
|
68
|
+
You can also specify a custom method with the keyword argument `on:`:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
class CustomError < StandardError
|
72
|
+
attr_reader :my_custom_attr
|
73
|
+
end
|
74
|
+
|
75
|
+
...
|
76
|
+
|
77
|
+
begin
|
78
|
+
raise CustomError.new('foo', 'some weird custom field')
|
79
|
+
rescue ME.matches(/weird/, on: :my_custom_attr)
|
80
|
+
puts 'yay!'
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
## Contributing
|
85
|
+
|
86
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/guava/matching_exceptions.
|
87
|
+
|
88
|
+
## License
|
89
|
+
|
90
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "matching_exceptions"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "matching_exceptions/version"
|
2
|
+
|
3
|
+
module MatchingExceptions
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def matches(matching, on: 'message')
|
7
|
+
klass = Class.new do
|
8
|
+
def self.===(other)
|
9
|
+
return false unless other.respond_to?(@method)
|
10
|
+
return other.send(@method).include?(@matching) if @matching.is_a?(String)
|
11
|
+
return other.send(@method) =~ @matching if @matching.is_a?(Regexp)
|
12
|
+
false
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
klass.instance_variable_set(:@matching, matching)
|
17
|
+
klass.instance_variable_set(:@method, on)
|
18
|
+
klass
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
ME = MatchingExceptions
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "matching_exceptions/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "matching_exceptions"
|
8
|
+
spec.version = MatchingExceptions::VERSION
|
9
|
+
spec.authors = ["Leonardo Brito", "Guilherme Carvalho"]
|
10
|
+
spec.email = ["leonardo@guava.com.br", "guilherme@guava.com.br"]
|
11
|
+
|
12
|
+
spec.summary = "Allows exceptions to be pattern matched"
|
13
|
+
spec.description = "Rescues exceptions using pattern matching"
|
14
|
+
spec.homepage = "https://github.com/guava/matching_exceptions"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: matching_exceptions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Leonardo Brito
|
8
|
+
- Guilherme Carvalho
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2017-08-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.15'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.15'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '3.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '3.0'
|
56
|
+
description: Rescues exceptions using pattern matching
|
57
|
+
email:
|
58
|
+
- leonardo@guava.com.br
|
59
|
+
- guilherme@guava.com.br
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".travis.yml"
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/matching_exceptions.rb
|
74
|
+
- lib/matching_exceptions/version.rb
|
75
|
+
- matching_exceptions.gemspec
|
76
|
+
homepage: https://github.com/guava/matching_exceptions
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.6.12
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Allows exceptions to be pattern matched
|
100
|
+
test_files: []
|