tries 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/tries/version.rb +1 -1
- data/lib/tries.rb +2 -0
- data/spec/tries/tries_spec.rb +18 -3
- metadata +2 -2
data/lib/tries/version.rb
CHANGED
data/lib/tries.rb
CHANGED
@@ -4,10 +4,12 @@ class Integer
|
|
4
4
|
def tries(options = {}, &block)
|
5
5
|
attempts = self
|
6
6
|
exception_classes = Array(options[:on] || StandardError)
|
7
|
+
delay = options[:delay]
|
7
8
|
|
8
9
|
begin
|
9
10
|
return yield
|
10
11
|
rescue *exception_classes
|
12
|
+
Kernel.sleep delay if delay
|
11
13
|
retry if (attempts -= 1) > 0
|
12
14
|
end
|
13
15
|
|
data/spec/tries/tries_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Tries do
|
4
4
|
before do
|
5
|
-
@
|
5
|
+
@error_counter = 0
|
6
6
|
end
|
7
7
|
|
8
8
|
context 'when retrying on all errors' do
|
@@ -58,15 +58,30 @@ describe Tries do
|
|
58
58
|
end.to raise_error(BarError)
|
59
59
|
end
|
60
60
|
end
|
61
|
+
|
62
|
+
context 'when specifying a delay' do
|
63
|
+
let(:delay) { 1.1 }
|
64
|
+
|
65
|
+
it 'sleeps the specified delay' do
|
66
|
+
Kernel.should_receive(:sleep).with(delay).exactly(2).times
|
67
|
+
|
68
|
+
begin
|
69
|
+
3.tries on: FooError, delay: delay do
|
70
|
+
raise_foo_foo_bar_bar_standard
|
71
|
+
end
|
72
|
+
rescue StandardError
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
61
76
|
end
|
62
77
|
|
63
78
|
FooError = Class.new(StandardError)
|
64
79
|
BarError = Class.new(StandardError)
|
65
80
|
|
66
81
|
def raise_foo_foo_bar_bar_standard
|
67
|
-
@
|
82
|
+
@error_counter += 1
|
68
83
|
|
69
|
-
case @
|
84
|
+
case @error_counter
|
70
85
|
when 1
|
71
86
|
raise FooError
|
72
87
|
when 2
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tries
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|