fail_retry 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b9ec875649f8634f074dd84df2ef76a0e876fef
4
- data.tar.gz: f67a65b0ca3a10500acfd9040138b94574335124
3
+ metadata.gz: 2760f74347d4d9702e99899cddd837e9a42c7942
4
+ data.tar.gz: 5e273349f25589aa96b231c63d8d5d33a62ca6f4
5
5
  SHA512:
6
- metadata.gz: aa379287c86b1d9573ef17f1dd5d37d3c76d03e9fae130781a6e2fd2c9aad44310afa6599fbcb7c64d7b65f143a7211244ddb20a6911e313756d0adb27ebb9ee
7
- data.tar.gz: 9fc8e81742601f1e61cb1536037d0e2f2f5e944edbfcf18db270f90fc60dabb27884dc60f771d479126bdfc8637d9901327289c9e1b7e6c6d6c2c70c74cebb7c
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, exception: Fail, max_tries: 1, on_retry: nil)
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
- raise if !e.kind_of?(exception) || trial >= max_tries
18
- on_retry.call(self) if on_retry
19
- trial += 1
20
- retry
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
 
@@ -1,3 +1,3 @@
1
1
  module FailRetry
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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, exception: ZeroDivisionError, on_retry: -> (instance) { instance.value += 1 }
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 max_retries is 0" do
55
+ context "when a method call fails and max is 0" do
55
56
  before do
56
- klass.fail_retry :call, exception: ZeroDivisionError, max_tries: 0
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, exception: ZeroDivisionError, max_tries: 3
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.1
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: '0'
85
+ version: '2.1'
86
86
  required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  requirements:
88
88
  - - ">="