rescata 0.1.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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +213 -0
- data/lib/rescata.rb +72 -0
- data/rescata.gemspec +20 -0
- data/test/test_rescata.rb +516 -0
- metadata +63 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 6630bf8886b66cdfdac2d8bf50faeaf12c77c939
|
|
4
|
+
data.tar.gz: 301a28bfb6323f4661c03986760ecb6b07eb2839
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c47d2249c54d3561923562e3f703bc80e56082111b4af19d552a9677ac35101cbff2921ed76bb3555ffe7032287fd4a95c73a51854315ca7a5d7493023a4405b
|
|
7
|
+
data.tar.gz: 47cceaa05f8533cb3a3b4d232a55220edaf93c091d68986ecb9436395bc73b2fea9ca4ed6aaaee8a899c3d19e9a8d363d9c050e74a62752fbd4291fc80397ca4
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Julio Lopez
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# rescata
|
|
2
|
+
Microlibrary for rescue exceptions declaratively in your Ruby classes.
|
|
3
|
+
|
|
4
|
+
Dry up your code from `begin... rescue... ensure.... end`
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
Installing Rescata is as simple as running:
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
$ gem install rescata
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Include Rescata in your Gemfile with gem 'rescata' or require it with require 'rescata'.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
### Rescuing from errors
|
|
19
|
+
|
|
20
|
+
After include `Rescata`, you can rescue any error inherited from `StandardError` passing to `rescata` the method that you want to rescue if something is raised inside of it, and the method you want to execute(in `with` key) for rescue it(both method's names as symbols).
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
class User
|
|
24
|
+
include Rescata
|
|
25
|
+
rescata :operation, with: :rescuing_operation
|
|
26
|
+
|
|
27
|
+
def operation
|
|
28
|
+
raise "some problem"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def rescuing_operation
|
|
32
|
+
"do_something"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Next line execute :rescuing_operation if a raise is launched inside of operation
|
|
37
|
+
# The error will be rescued
|
|
38
|
+
puts User.new.operation
|
|
39
|
+
#=> "do_something"
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
You can get the error instance if your method derived to rescue can receive an argument.
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
class User
|
|
46
|
+
include Rescata
|
|
47
|
+
rescata :operation, with: :rescuing_operation
|
|
48
|
+
|
|
49
|
+
def operation
|
|
50
|
+
raise "a problem"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def rescuing_operation(e)
|
|
54
|
+
"is happening #{e.message}"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
puts User.new.operation
|
|
59
|
+
#=> "is happening a problem"
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Rescuing from an error class
|
|
63
|
+
|
|
64
|
+
Also is possible rescue just from a particular error class, just use `in` key as a hash argument into `rescata` to make that possible.
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
class User
|
|
68
|
+
include Rescata
|
|
69
|
+
rescata :operation, with: :rescuing_operation, in: ArgumentError
|
|
70
|
+
rescata :other_operation, with: :rescuing_operation, in: NameError
|
|
71
|
+
|
|
72
|
+
def operation
|
|
73
|
+
raise "a problem"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def other_operation
|
|
77
|
+
raise NameError, "other problem"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def rescuing_operation(e)
|
|
81
|
+
"is happening #{e.message}"
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# This will be raise an error(in the example above we are just recuing 'operation' from ArgumentError)
|
|
86
|
+
User.new.operation
|
|
87
|
+
#=> RuntimeError: a problem
|
|
88
|
+
|
|
89
|
+
# This will be rescued
|
|
90
|
+
puts User.new.other_operation
|
|
91
|
+
#=> "is happening other problem"
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Send an array if you want to rescue for two or more class errors.
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
class User
|
|
98
|
+
include Rescata
|
|
99
|
+
rescata :operation, with: :rescuing_operation, in: [NameError, ArgumentError]
|
|
100
|
+
|
|
101
|
+
def operation
|
|
102
|
+
raise ArgumentError, "a problem"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def rescuing_operation(e)
|
|
106
|
+
"is happening #{e.message}"
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# This will be rescued because ArgumentError is included into the error classes specified
|
|
111
|
+
puts User.new.operation
|
|
112
|
+
#=> "is happening other problem"
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Rescuing using lambdas and blocks
|
|
116
|
+
|
|
117
|
+
You can rescue a method using a lambda. Just add it into the `with` key(don't forget receive the error instance if you need it). Or use a block if you want.
|
|
118
|
+
|
|
119
|
+
```ruby
|
|
120
|
+
class User
|
|
121
|
+
include Rescata
|
|
122
|
+
# Rescuing using a lambda
|
|
123
|
+
rescata :operation, with: lambda{|e| "is happening #{e.message}" }
|
|
124
|
+
|
|
125
|
+
# Rescuing using a block
|
|
126
|
+
rescata :other_operation do |e|
|
|
127
|
+
"is happening #{e.message}"
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def operation
|
|
131
|
+
raise "a problem"
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def other_operation
|
|
135
|
+
raise "other problem"
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# both methods will be rescued
|
|
140
|
+
puts User.new.operation
|
|
141
|
+
#=> "is happening a problem"
|
|
142
|
+
|
|
143
|
+
puts User.new.other_operation
|
|
144
|
+
#=> "is happening other problem"
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Also you can still rescuing from particular errors using lambdas or blocks. This gives you freedom to build any custom solution to rescue for specific error classes.
|
|
148
|
+
|
|
149
|
+
```ruby
|
|
150
|
+
rescata :operation, with: :rescuing_operation, in: CustomError
|
|
151
|
+
rescata :operation, with: lambda{|e| do_something }, in: RuntimeError
|
|
152
|
+
rescata :operation, in: [ArgumentError, NameError] do |e|
|
|
153
|
+
do_something
|
|
154
|
+
end
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Rescuing multiple methods at once
|
|
158
|
+
|
|
159
|
+
Rescue from multiple methods in the same line sending them into an array as first variable of `rescata`.
|
|
160
|
+
|
|
161
|
+
```ruby
|
|
162
|
+
# Rescuing multiple methods in many ways
|
|
163
|
+
rescata [:operation, :other_operation], with: :rescuing_operations
|
|
164
|
+
rescata [:operation, :other_operation], with: lambda{|e| do_something }, in: CustomError
|
|
165
|
+
rescata [:operation, :other_operation], in: [CustomError, NameError] do |e|
|
|
166
|
+
do_something
|
|
167
|
+
end
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Ensuring
|
|
171
|
+
|
|
172
|
+
And we haven't forgotten the ensure actions, you can ensure your methods using `ensuring` key as a hash argument into `rescata`, you can ensure from a method or a lambda, if you opt for lambda it will receive the instance of your class as a variable to do whatever you need.
|
|
173
|
+
|
|
174
|
+
```ruby
|
|
175
|
+
class User
|
|
176
|
+
attr_accessor :name
|
|
177
|
+
|
|
178
|
+
include Rescata
|
|
179
|
+
rescata :operation, with: :rescuing_operation, ensuring: :ensuring_method
|
|
180
|
+
rescata :other_operation, with: :rescuing_operation, ensuring: lambda{|instance| instance.name = "Piero" }
|
|
181
|
+
|
|
182
|
+
def initialize(name)
|
|
183
|
+
@name = name
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def operation
|
|
187
|
+
raise "a problem"
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def other_operation
|
|
191
|
+
raise "other problem"
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def rescuing_operation
|
|
195
|
+
#do_something
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def ensuring_method
|
|
199
|
+
@name = "Piero"
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# Both methods will be rescued and executed its ensure actions
|
|
204
|
+
u = User.new("Julio")
|
|
205
|
+
u.operation
|
|
206
|
+
puts u.name
|
|
207
|
+
#=> "Piero"
|
|
208
|
+
|
|
209
|
+
u = User.new("Julio")
|
|
210
|
+
u.other_operation
|
|
211
|
+
puts u.name
|
|
212
|
+
#=> "Piero"
|
|
213
|
+
```
|
data/lib/rescata.rb
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Copyright (c) 2015 Julio Lopez
|
|
2
|
+
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
# furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
# copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
# SOFTWARE.
|
|
20
|
+
module Rescata
|
|
21
|
+
VERSION = "0.1.1"
|
|
22
|
+
|
|
23
|
+
def self.included(base)
|
|
24
|
+
class << base; attr_accessor :rescues; end
|
|
25
|
+
base.extend ClassMethods
|
|
26
|
+
base.rescues = {}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
module ClassMethods
|
|
30
|
+
def rescata(methods, options = {}, &block)
|
|
31
|
+
error_classes = Array(options[:in])
|
|
32
|
+
error_classes.each do |klass|
|
|
33
|
+
raise ArgumentError, 'Error class must be an Exception or sub-class' if klass.is_a?(Class) ? (klass <= Exception).nil? : true
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
options[:with] = block if block_given?
|
|
37
|
+
raise ArgumentError, 'Rescuer is incorrectly, supply it like a Method or a Proc with a hash with key :with as an argument' unless options[:with] && (options[:with].is_a?(Symbol) || options[:with].is_a?(Proc))
|
|
38
|
+
|
|
39
|
+
Array(methods).each do |method_name|
|
|
40
|
+
rescues[method_name] ||= []
|
|
41
|
+
rescues[method_name] << {
|
|
42
|
+
rescuer: options[:with]
|
|
43
|
+
}.merge(options[:in] ? {error_class: error_classes} : {})
|
|
44
|
+
.merge(options[:ensuring] ? {ensurer: options[:ensuring]} : {})
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def method_added(method_name)
|
|
49
|
+
return unless rescues && collection = rescues[method_name]
|
|
50
|
+
alias_method_name = :"rescuing_old_#{method_name}"
|
|
51
|
+
return if instance_methods.include?(alias_method_name)
|
|
52
|
+
alias_method alias_method_name, method_name
|
|
53
|
+
define_method method_name do
|
|
54
|
+
begin
|
|
55
|
+
send(alias_method_name)
|
|
56
|
+
rescue => e
|
|
57
|
+
handler = collection.select{|i| i[:error_class] ? i[:error_class].include?(e.class) : true }.first
|
|
58
|
+
rescuer, error_classes, ensurer = handler[:rescuer], handler[:error_class], handler[:ensurer]
|
|
59
|
+
raise e if error_classes && !error_classes.include?(e.class)
|
|
60
|
+
case rescuer
|
|
61
|
+
when Symbol
|
|
62
|
+
method(rescuer).arity == 0 ? send(rescuer) : send(rescuer, e)
|
|
63
|
+
when Proc
|
|
64
|
+
rescuer.arity == 0 ? rescuer.call : rescuer.call(e)
|
|
65
|
+
end
|
|
66
|
+
ensure
|
|
67
|
+
ensurer.is_a?(Symbol) ? send(ensurer) : ensurer.call(self) if ensurer
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
data/rescata.gemspec
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require "./lib/rescata"
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = "rescata"
|
|
5
|
+
s.version = Rescata::VERSION
|
|
6
|
+
s.summary = "Microlibrary for rescue exceptions declaratively in your Ruby classes."
|
|
7
|
+
s.description = "Microlibrary for rescue exceptions declaratively in your Ruby classes."
|
|
8
|
+
s.authors = ["Julio Lopez"]
|
|
9
|
+
s.email = ["ljuliom@gmail.com"]
|
|
10
|
+
s.homepage = "http://github.com/TheBlasfem/rescata"
|
|
11
|
+
s.files = Dir[
|
|
12
|
+
"LICENSE",
|
|
13
|
+
"README.md",
|
|
14
|
+
"lib/**/*.rb",
|
|
15
|
+
"*.gemspec",
|
|
16
|
+
"test/**/*.rb"
|
|
17
|
+
]
|
|
18
|
+
s.license = "MIT"
|
|
19
|
+
s.add_development_dependency "cutest", "1.1.3"
|
|
20
|
+
end
|
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
require File.expand_path("../lib/rescata", File.dirname(__FILE__))
|
|
2
|
+
|
|
3
|
+
scope do
|
|
4
|
+
test "raise if rescuer method is not sent" do
|
|
5
|
+
assert_raise(ArgumentError) do
|
|
6
|
+
Class.new do
|
|
7
|
+
include Rescata
|
|
8
|
+
rescata :get_talks
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
test "rescue an error with method" do
|
|
14
|
+
User = Class.new do
|
|
15
|
+
include Rescata
|
|
16
|
+
rescata :get_talks, with: :rescue_get_talks
|
|
17
|
+
|
|
18
|
+
def get_talks
|
|
19
|
+
raise "throwing an error!"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def rescue_get_talks
|
|
23
|
+
"rescued!"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
assert_equal User.new.get_talks, "rescued!"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
test "rescue an error with method passing the error variable" do
|
|
31
|
+
User = Class.new do
|
|
32
|
+
include Rescata
|
|
33
|
+
rescata :get_talks, with: :rescue_get_talks
|
|
34
|
+
|
|
35
|
+
def get_talks
|
|
36
|
+
raise "i want"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def rescue_get_talks(e)
|
|
40
|
+
"rescued because #{e.message}"
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
assert_equal User.new.get_talks, "rescued because i want"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
test "raise if rescuer is not a method or a proc" do
|
|
48
|
+
assert_raise(ArgumentError) do
|
|
49
|
+
Class.new do
|
|
50
|
+
include Rescata
|
|
51
|
+
rescata :get_talks, with: "whatever"
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
test "raise if error class sent is not a class" do
|
|
57
|
+
assert_raise(ArgumentError) do
|
|
58
|
+
Class.new do
|
|
59
|
+
include Rescata
|
|
60
|
+
rescata :get_talks, with: :rescue_get_talks, in: "whatever"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
test "raise if error class sent is not a Exception class or subclass" do
|
|
66
|
+
assert_raise(ArgumentError) do
|
|
67
|
+
Class.new do
|
|
68
|
+
include Rescata
|
|
69
|
+
rescata :get_talks, with: :rescue_get_talks, in: User
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
test "rescue from a class error" do
|
|
75
|
+
User = Class.new do
|
|
76
|
+
include Rescata
|
|
77
|
+
rescata :get_talks, with: :rescue_get_talks, in: ArgumentError
|
|
78
|
+
|
|
79
|
+
def get_talks
|
|
80
|
+
raise ArgumentError
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def rescue_get_talks
|
|
84
|
+
"rescued!"
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
assert_equal User.new.get_talks, "rescued!"
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
test "rescue with method from a class error passing error variable" do
|
|
92
|
+
User = Class.new do
|
|
93
|
+
include Rescata
|
|
94
|
+
rescata :get_talks, with: :rescue_get_talks, in: ArgumentError
|
|
95
|
+
|
|
96
|
+
def get_talks
|
|
97
|
+
raise ArgumentError, "i want"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def rescue_get_talks(e)
|
|
101
|
+
"rescued because #{e.message}"
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
assert_equal User.new.get_talks, "rescued because i want"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
test "raise if different class error raises" do
|
|
109
|
+
User = Class.new do
|
|
110
|
+
include Rescata
|
|
111
|
+
rescata :get_talks, with: :rescue_get_talks, in: NameError
|
|
112
|
+
|
|
113
|
+
def get_talks
|
|
114
|
+
raise StandardError
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def rescue_get_talks
|
|
118
|
+
"rescued!"
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
assert_raise(StandardError) do
|
|
123
|
+
User.new.get_talks
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
test "raise if error class sent in array is not a class" do
|
|
128
|
+
assert_raise(ArgumentError) do
|
|
129
|
+
Class.new do
|
|
130
|
+
include Rescata
|
|
131
|
+
rescata :get_talks, with: :rescue_get_talks, in: [NameError, "whatever"]
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
test "raise if error class sent in array is not a Exception class or subclass" do
|
|
137
|
+
assert_raise(ArgumentError) do
|
|
138
|
+
Class.new do
|
|
139
|
+
include Rescata
|
|
140
|
+
rescata :get_talks, with: :rescue_get_talks, in: [NameError, User]
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
test "rescue with method from a sent array of error classes" do
|
|
146
|
+
User = Class.new do
|
|
147
|
+
include Rescata
|
|
148
|
+
rescata :get_talks, with: :rescue_get_talks, in: [NameError, ArgumentError]
|
|
149
|
+
|
|
150
|
+
def get_talks
|
|
151
|
+
raise ArgumentError
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def rescue_get_talks
|
|
155
|
+
"rescued!"
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
assert_equal User.new.get_talks, "rescued!"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
test "raise if different class error raise from array of error classes" do
|
|
163
|
+
User = Class.new do
|
|
164
|
+
include Rescata
|
|
165
|
+
rescata :get_talks, with: :rescue_get_talks, in: [NameError, ArgumentError]
|
|
166
|
+
|
|
167
|
+
def get_talks
|
|
168
|
+
raise StandardError
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def rescue_get_talks
|
|
172
|
+
"rescued!"
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
assert_raise(StandardError) do
|
|
177
|
+
User.new.get_talks
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
test "rescue an error with proc" do
|
|
182
|
+
User = Class.new do
|
|
183
|
+
include Rescata
|
|
184
|
+
rescata :get_talks, with: lambda{"rescued"}
|
|
185
|
+
|
|
186
|
+
def get_talks
|
|
187
|
+
raise "throwing an error"
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
assert_equal User.new.get_talks, "rescued"
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
test "rescue an error with proc sending error variable" do
|
|
195
|
+
User = Class.new do
|
|
196
|
+
include Rescata
|
|
197
|
+
rescata :get_talks, with: lambda{|e| "rescued because #{e.message}"}
|
|
198
|
+
|
|
199
|
+
def get_talks
|
|
200
|
+
raise "i want"
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
assert_equal User.new.get_talks, "rescued because i want"
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
test "rescue with proc from a class error passing error variable" do
|
|
208
|
+
User = Class.new do
|
|
209
|
+
include Rescata
|
|
210
|
+
rescata :get_talks, with: lambda{|e| "rescued because #{e.message}"}, in: ArgumentError
|
|
211
|
+
|
|
212
|
+
def get_talks
|
|
213
|
+
raise ArgumentError, "i want"
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
assert_equal User.new.get_talks, "rescued because i want"
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
test "rescue with proc from a sent array of error classes" do
|
|
221
|
+
User = Class.new do
|
|
222
|
+
include Rescata
|
|
223
|
+
rescata :get_talks, with: lambda{"rescued"}, in: [NameError, ArgumentError]
|
|
224
|
+
|
|
225
|
+
def get_talks
|
|
226
|
+
raise ArgumentError
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
assert_equal User.new.get_talks, "rescued"
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
test "rescue an error with block" do
|
|
234
|
+
User = Class.new do
|
|
235
|
+
include Rescata
|
|
236
|
+
rescata :get_talks do
|
|
237
|
+
"rescued"
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def get_talks
|
|
241
|
+
raise "throwing an error"
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
assert_equal User.new.get_talks, "rescued"
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
test "rescue an error with block sending error variable" do
|
|
249
|
+
User = Class.new do
|
|
250
|
+
include Rescata
|
|
251
|
+
rescata :get_talks do |e|
|
|
252
|
+
"rescued because #{e.message}"
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def get_talks
|
|
256
|
+
raise "i want"
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
assert_equal User.new.get_talks, "rescued because i want"
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
test "rescue with block from a class error passing error variable" do
|
|
264
|
+
User = Class.new do
|
|
265
|
+
include Rescata
|
|
266
|
+
rescata :get_talks, in: ArgumentError do |e|
|
|
267
|
+
"rescued because #{e.message}"
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def get_talks
|
|
271
|
+
raise ArgumentError, "i want"
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
assert_equal User.new.get_talks, "rescued because i want"
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
test "rescue with block from a sent array of error classes" do
|
|
279
|
+
User = Class.new do
|
|
280
|
+
include Rescata
|
|
281
|
+
rescata :get_talks, in: [NameError, ArgumentError] do
|
|
282
|
+
"rescued"
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def get_talks
|
|
286
|
+
raise ArgumentError
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
assert_equal User.new.get_talks, "rescued"
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
test "no screw up with child classes" do
|
|
294
|
+
User = Class.new do
|
|
295
|
+
include Rescata
|
|
296
|
+
rescata :get_talks, with: :rescue_get_talks
|
|
297
|
+
|
|
298
|
+
def get_talks
|
|
299
|
+
raise "OH NOOO"
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
def rescue_get_talks
|
|
303
|
+
"yep"
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
Student = Class.new(User) do
|
|
308
|
+
def demo
|
|
309
|
+
"hi"
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
assert_equal Student.new.demo, "hi"
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
test "rescuing from method of parent class" do
|
|
317
|
+
User = Class.new do
|
|
318
|
+
include Rescata
|
|
319
|
+
rescata :get_talks, with: :rescue_get_talks
|
|
320
|
+
|
|
321
|
+
def get_talks
|
|
322
|
+
raise "OH NOOO"
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
def rescue_get_talks
|
|
326
|
+
"yep"
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
Student = Class.new(User)
|
|
331
|
+
assert_equal Student.new.get_talks, "yep"
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
test "rescuing multiples methods at once with a method" do
|
|
335
|
+
User = Class.new do
|
|
336
|
+
include Rescata
|
|
337
|
+
rescata [:get_talks, :other_method], with: :rescue_get_talks
|
|
338
|
+
|
|
339
|
+
def get_talks
|
|
340
|
+
raise "OH NOOO"
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def other_method
|
|
344
|
+
raise "OH NOOO"
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
def rescue_get_talks
|
|
348
|
+
"yep"
|
|
349
|
+
end
|
|
350
|
+
end
|
|
351
|
+
assert_equal User.new.get_talks, "yep"
|
|
352
|
+
assert_equal User.new.other_method, "yep"
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
test "rescuing multiples methods at once with a method and particular class error" do
|
|
356
|
+
User = Class.new do
|
|
357
|
+
include Rescata
|
|
358
|
+
rescata [:get_talks, :other_method], with: :rescue_get_talks, in: ArgumentError
|
|
359
|
+
|
|
360
|
+
def get_talks
|
|
361
|
+
raise StandardError, "OH NOOO"
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def other_method
|
|
365
|
+
raise ArgumentError, "OH NOOO"
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def rescue_get_talks
|
|
369
|
+
"yep"
|
|
370
|
+
end
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
assert_raise(StandardError) do
|
|
374
|
+
User.new.get_talks
|
|
375
|
+
end
|
|
376
|
+
assert_equal User.new.other_method, "yep"
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
test "rescuing multiples methods at once with lambda and particular class error" do
|
|
380
|
+
User = Class.new do
|
|
381
|
+
include Rescata
|
|
382
|
+
rescata [:get_talks, :other_method], with: lambda{|e| "#{e.message} i want"}, in: ArgumentError
|
|
383
|
+
|
|
384
|
+
def get_talks
|
|
385
|
+
raise StandardError, "OH NOOO"
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
def other_method
|
|
389
|
+
raise ArgumentError, "raised because"
|
|
390
|
+
end
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
assert_raise(StandardError) do
|
|
394
|
+
User.new.get_talks
|
|
395
|
+
end
|
|
396
|
+
assert_equal User.new.other_method, "raised because i want"
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
test "rescuing multiples methods at once with block and particular class error" do
|
|
400
|
+
User = Class.new do
|
|
401
|
+
include Rescata
|
|
402
|
+
rescata [:get_talks, :other_method], in: ArgumentError do |e|
|
|
403
|
+
"#{e.message} i want"
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
def get_talks
|
|
407
|
+
raise StandardError, "OH NOOO"
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
def other_method
|
|
411
|
+
raise ArgumentError, "raised because"
|
|
412
|
+
end
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
assert_raise(StandardError) do
|
|
416
|
+
User.new.get_talks
|
|
417
|
+
end
|
|
418
|
+
assert_equal User.new.other_method, "raised because i want"
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
test "rescuing same method with multiple rescatas as first assert" do
|
|
422
|
+
User = Class.new do
|
|
423
|
+
include Rescata
|
|
424
|
+
rescata :get_talks, with: :rescue_get_talks, in: StandardError
|
|
425
|
+
rescata :get_talks, with: :rescue_get_talks2, in: ArgumentError
|
|
426
|
+
|
|
427
|
+
def get_talks
|
|
428
|
+
raise StandardError, "OH NOOO"
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
def rescue_get_talks
|
|
432
|
+
"yep"
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
def rescue_get_talks2
|
|
436
|
+
"yep2"
|
|
437
|
+
end
|
|
438
|
+
end
|
|
439
|
+
|
|
440
|
+
assert_equal User.new.get_talks, "yep"
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
test "rescuing same method with multiple rescatas as last assert" do
|
|
444
|
+
User = Class.new do
|
|
445
|
+
include Rescata
|
|
446
|
+
rescata :get_talks, with: :rescue_get_talks, in: StandardError
|
|
447
|
+
rescata :get_talks, with: :rescue_get_talks2, in: ArgumentError
|
|
448
|
+
|
|
449
|
+
def get_talks
|
|
450
|
+
raise ArgumentError, "OH NOOO"
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
def rescue_get_talks
|
|
454
|
+
"yep"
|
|
455
|
+
end
|
|
456
|
+
|
|
457
|
+
def rescue_get_talks2
|
|
458
|
+
"yep2"
|
|
459
|
+
end
|
|
460
|
+
end
|
|
461
|
+
|
|
462
|
+
assert_equal User.new.get_talks, "yep2"
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
test "ensure with a method" do
|
|
466
|
+
User = Class.new do
|
|
467
|
+
include Rescata
|
|
468
|
+
rescata :get_talks, with: :rescue_get_talks, ensuring: :ensure_method
|
|
469
|
+
|
|
470
|
+
attr_accessor :x
|
|
471
|
+
def initialize
|
|
472
|
+
@x = 1
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
def get_talks
|
|
476
|
+
raise "throwing an error!"
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
def rescue_get_talks
|
|
480
|
+
"rescued!"
|
|
481
|
+
end
|
|
482
|
+
|
|
483
|
+
def ensure_method
|
|
484
|
+
@x += 1
|
|
485
|
+
end
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
u = User.new
|
|
489
|
+
u.get_talks
|
|
490
|
+
assert_equal u.x, 2
|
|
491
|
+
end
|
|
492
|
+
|
|
493
|
+
test "ensure with a lambda" do
|
|
494
|
+
User = Class.new do
|
|
495
|
+
include Rescata
|
|
496
|
+
rescata :get_talks, with: :rescue_get_talks, ensuring: lambda{|u| u.x += 1 }
|
|
497
|
+
|
|
498
|
+
attr_accessor :x
|
|
499
|
+
def initialize
|
|
500
|
+
@x = 1
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
def get_talks
|
|
504
|
+
raise "throwing an error!"
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
def rescue_get_talks
|
|
508
|
+
"rescued!"
|
|
509
|
+
end
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
u = User.new
|
|
513
|
+
u.get_talks
|
|
514
|
+
assert_equal u.x, 2
|
|
515
|
+
end
|
|
516
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rescata
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Julio Lopez
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-07-04 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: cutest
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 1.1.3
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 1.1.3
|
|
27
|
+
description: Microlibrary for rescue exceptions declaratively in your Ruby classes.
|
|
28
|
+
email:
|
|
29
|
+
- ljuliom@gmail.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- LICENSE
|
|
35
|
+
- README.md
|
|
36
|
+
- lib/rescata.rb
|
|
37
|
+
- rescata.gemspec
|
|
38
|
+
- test/test_rescata.rb
|
|
39
|
+
homepage: http://github.com/TheBlasfem/rescata
|
|
40
|
+
licenses:
|
|
41
|
+
- MIT
|
|
42
|
+
metadata: {}
|
|
43
|
+
post_install_message:
|
|
44
|
+
rdoc_options: []
|
|
45
|
+
require_paths:
|
|
46
|
+
- lib
|
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '0'
|
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: '0'
|
|
57
|
+
requirements: []
|
|
58
|
+
rubyforge_project:
|
|
59
|
+
rubygems_version: 2.4.5
|
|
60
|
+
signing_key:
|
|
61
|
+
specification_version: 4
|
|
62
|
+
summary: Microlibrary for rescue exceptions declaratively in your Ruby classes.
|
|
63
|
+
test_files: []
|