fail_retry 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/fail_retry.gemspec +2 -0
- data/lib/fail_retry.rb +13 -5
- data/lib/fail_retry/version.rb +1 -1
- data/spec/fail_retry_spec.rb +5 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2760f74347d4d9702e99899cddd837e9a42c7942
|
4
|
+
data.tar.gz: 5e273349f25589aa96b231c63d8d5d33a62ca6f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 699c4f950ee5bbafb54d6f2b5528dafa04fa144296d769d4b4b852c5fc3988ed0c1c6f19c9d0492151981f261805b6b18d8042d91db72102196cc0124433c15f
|
7
|
+
data.tar.gz: 539c2e330209276a7153ef4bb095ed769045833120fdc4371f1c7a31614c34010769acfdc52916614f58721ea8bf62a828bf692b404be1badd146d9bc847ba4b
|
data/fail_retry.gemspec
CHANGED
@@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
+
spec.required_ruby_version = "~> 2.1"
|
21
|
+
|
20
22
|
spec.add_development_dependency "bundler", "~>1.7"
|
21
23
|
spec.add_development_dependency "rake", "~>10.0"
|
22
24
|
spec.add_development_dependency "rspec", "2.14.1"
|
data/lib/fail_retry.rb
CHANGED
@@ -8,16 +8,24 @@ module FailRetry
|
|
8
8
|
end
|
9
9
|
|
10
10
|
module ClassMethods
|
11
|
-
def fail_retry(method_name,
|
11
|
+
def fail_retry(method_name, on: Fail, max: 1, if: nil)
|
12
12
|
define_method("#{method_name}_with_retry") do |*args, &block|
|
13
13
|
trial = 0
|
14
14
|
begin
|
15
15
|
send("#{method_name}_without_retry", *args, &block)
|
16
16
|
rescue => e
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
exception = on
|
18
|
+
if_proc = binding.local_variable_get(:if)
|
19
|
+
retriable = e.kind_of?(exception)
|
20
|
+
retriable &&= trial < max
|
21
|
+
retriable &&= if_proc ? if_proc.call(self) : true
|
22
|
+
|
23
|
+
if retriable
|
24
|
+
trial = trial.succ
|
25
|
+
retry
|
26
|
+
else
|
27
|
+
raise
|
28
|
+
end
|
21
29
|
end
|
22
30
|
end
|
23
31
|
|
data/lib/fail_retry/version.rb
CHANGED
data/spec/fail_retry_spec.rb
CHANGED
@@ -33,7 +33,7 @@ describe FailRetry do
|
|
33
33
|
|
34
34
|
context "when a method call succeeds" do
|
35
35
|
before do
|
36
|
-
klass.fail_retry :call,
|
36
|
+
klass.fail_retry :call, on: ZeroDivisionError, if: -> (instance) { instance.value += 1 }
|
37
37
|
end
|
38
38
|
|
39
39
|
it "retries and return number" do
|
@@ -43,6 +43,7 @@ describe FailRetry do
|
|
43
43
|
|
44
44
|
context "when an unexpected exception happened" do
|
45
45
|
before do
|
46
|
+
klass.fail_retry :call, on: ArgumentError
|
46
47
|
klass.send(:undef_method, :perform_action)
|
47
48
|
end
|
48
49
|
|
@@ -51,9 +52,9 @@ describe FailRetry do
|
|
51
52
|
end
|
52
53
|
end
|
53
54
|
|
54
|
-
context "when a method call fails and
|
55
|
+
context "when a method call fails and max is 0" do
|
55
56
|
before do
|
56
|
-
klass.fail_retry :call,
|
57
|
+
klass.fail_retry :call, on: ZeroDivisionError, max: 0
|
57
58
|
end
|
58
59
|
|
59
60
|
it "retries without retry" do
|
@@ -64,7 +65,7 @@ describe FailRetry do
|
|
64
65
|
|
65
66
|
context "when a method call fails until trial reaches max_tries" do
|
66
67
|
before do
|
67
|
-
klass.fail_retry :call,
|
68
|
+
klass.fail_retry :call, on: ZeroDivisionError, max: 3
|
68
69
|
end
|
69
70
|
|
70
71
|
it "retries and raise error" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fail_retry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomoya Hirano
|
@@ -80,9 +80,9 @@ require_paths:
|
|
80
80
|
- lib
|
81
81
|
required_ruby_version: !ruby/object:Gem::Requirement
|
82
82
|
requirements:
|
83
|
-
- - "
|
83
|
+
- - "~>"
|
84
84
|
- !ruby/object:Gem::Version
|
85
|
-
version: '
|
85
|
+
version: '2.1'
|
86
86
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
87
|
requirements:
|
88
88
|
- - ">="
|