wait 0.3.2 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/wait.rb +59 -40
  2. metadata +2 -2
data/lib/wait.rb CHANGED
@@ -2,59 +2,63 @@ require File.expand_path("../initialize", __FILE__)
2
2
 
3
3
  class Wait
4
4
  DEFAULT = {
5
- :timeout => 15,
6
- :logger => BaseLogger,
7
5
  :attempts => 5,
8
- :counter => BaseCounter,
6
+ :timeout => 15,
9
7
  :delay => 1,
8
+ :counter => BaseCounter,
10
9
  :delayer => RegularDelayer,
10
+ :rescuer => BaseRescuer,
11
11
  :tester => TruthyTester,
12
- :rescuer => PassiveRescuer
12
+ :raiser => PassiveRaiser,
13
+ :logger => BaseLogger
13
14
  }
14
15
 
15
16
  # Creates a new Wait instance.
16
17
  #
17
- # == Options
18
+ # == Basic Options
18
19
  #
19
20
  # [:attempts]
20
- # Number of times to attempt the block (passed to +counter+). Default is
21
- # +5+.
22
- # [:counter]
23
- # Strategy used to count attempts. Default is Wait::BaseCounter.
21
+ # Number of times to attempt the block. Default is +5+.
24
22
  # [:timeout]
25
23
  # Seconds until the block times out. Default is +15+.
26
24
  # [:delay]
27
- # Seconds to delay in between attempts (passed to +delayer+). Default is
28
- # +1+.
25
+ # Seconds to delay in between attempts. Default is +1+.
26
+ # [:rescue]
27
+ # One or an array of exceptions to rescue. Default is +nil+.
28
+ # [:debug]
29
+ # If +true+, debug logging is enabled. Default is +false+.
30
+ #
31
+ # == Advanced Options
32
+ #
33
+ # [:logger]
34
+ # Ruby logger used. Default is Wait::BaseLogger.
35
+ # [:counter]
36
+ # Strategy used to count attempts. Default is Wait::BaseCounter.
29
37
  # [:delayer]
30
38
  # Strategy used to delay in between attempts. Default is
31
39
  # Wait::RegularDelayer.
32
- # [:rescue]
33
- # One or an array of exceptions to rescue (passed to +rescuer+). Default
34
- # is +nil+.
35
40
  # [:rescuer]
36
- # Strategy used to handle exceptions. Default is Wait::PassiveRescuer.
41
+ # Strategy used to rescue exceptions. Default is Wait::BaseRescuer.
37
42
  # [:tester]
38
43
  # Strategy used to test the result. Default is Wait::TruthyTester.
39
- # [:logger]
40
- # Ruby logger used. Default is Wait::BaseLogger.
44
+ # [:raiser]
45
+ # Strategy used to raise specific exceptions. Default is
46
+ # Wait::PassiveRaiser.
41
47
  #
42
48
  def initialize(options = {})
43
- @timeout = options[:timeout] || DEFAULT[:timeout]
44
- debug = options[:debug]
45
- @logger = (options[:logger] || (debug ? DebugLogger : DEFAULT[:logger])).new
46
- attempts = options[:attempts] || DEFAULT[:attempts]
47
- @counter = (options[:counter] || DEFAULT[:counter]).new(@logger, attempts)
48
- delay = options[:delay] || DEFAULT[:delay]
49
- @delayer = (options[:delayer] || DEFAULT[:delayer]).new(@logger, delay)
50
- @tester = (options[:tester] || DEFAULT[:tester]).new(@logger)
51
- exceptions = Array(options[:rescue])
52
- @rescuer = (options[:rescuer] || DEFAULT[:rescuer]).new(@logger, @tester.exceptions, exceptions)
49
+ debug = options[:debug] || false
50
+ @logger = (options[:logger] || (debug ? DebugLogger : DEFAULT[:logger])).new
51
+ attempts = options[:attempts] || DEFAULT[:attempts]
52
+ @counter = (options[:counter] || DEFAULT[:counter]).new(@logger, attempts)
53
+ @timeout = options[:timeout] || DEFAULT[:timeout]
54
+ delay = options[:delay] || DEFAULT[:delay]
55
+ @delayer = (options[:delayer] || DEFAULT[:delayer]).new(@logger, delay)
56
+ exceptions = options[:rescue]
57
+ @rescuer = (options[:rescuer] || DEFAULT[:rescuer]).new(@logger, exceptions)
58
+ @tester = options[:tester] || DEFAULT[:tester]
59
+ @raiser = options[:raiser] || DEFAULT[:raiser]
53
60
  end
54
61
 
55
- # Raised when a block times out.
56
- class TimeoutError < Timeout::Error; end
57
-
58
62
  # == Description
59
63
  #
60
64
  # Wait#until executes a block until there's a valid (by default, truthy)
@@ -69,10 +73,12 @@ class Wait
69
73
  # wait = Wait.new
70
74
  # # => #<Wait>
71
75
  # wait.until { Time.now.sec.even? }
76
+ # # [Counter] attempt 1/5
72
77
  # # [Tester] result: false
73
- # # [Rescuer] rescued: Wait::TruthyTester::ResultNotTruthy: false
74
- # # [Counter] attempt 1/5 failed
78
+ # # [Rescuer] rescued: Wait::InvalidResult: Wait::InvalidResult
79
+ # # [Raiser] raise? Wait::InvalidResult: false
75
80
  # # [Delayer] delaying for 1s
81
+ # # [Counter] attempt 2/5
76
82
  # # [Tester] result: true
77
83
  # # => true
78
84
  #
@@ -88,13 +94,16 @@ class Wait
88
94
  # when 3 then "foo"
89
95
  # end
90
96
  # end
97
+ # # [Counter] attempt 1/5
91
98
  # # [Tester] result: nil
92
- # # [Rescuer] rescued: Wait::TruthyTester::ResultNotTruthy: nil
93
- # # [Counter] attempt 1/5 failed
99
+ # # [Rescuer] rescued: Wait::InvalidResult: Wait::InvalidResult
100
+ # # [Raiser] raise? Wait::InvalidResult: false
94
101
  # # [Delayer] delaying for 1s
102
+ # # [Counter] attempt 2/5
95
103
  # # [Rescuer] rescued: RuntimeError: RuntimeError
96
- # # [Counter] attempt 2/5 failed
104
+ # # [Raiser] raise? RuntimeError: false
97
105
  # # [Delayer] delaying for 1s
106
+ # # [Counter] attempt 3/5
98
107
  # # [Tester] result: "foo"
99
108
  # # => "foo"
100
109
  #
@@ -118,16 +127,26 @@ class Wait
118
127
  yield(@counter.attempt)
119
128
  end
120
129
  # Raise an exception unless the result is valid.
121
- @tester.raise_unless_valid(result)
122
- rescue *@rescuer.exceptions => exception
123
- # Raise the exception unless it can be ignored.
124
- @rescuer.raise_unless_ignore(exception)
130
+ tester = @tester.new(@logger, result)
131
+ tester.valid? ? result : raise(InvalidResult)
132
+ rescue TimeoutError, InvalidResult, *@rescuer.exceptions => exception
133
+ # Log the exception.
134
+ @rescuer.log(exception)
135
+ # Raise the exception if it ought to be.
136
+ raiser = @raiser.new(@logger, exception)
137
+ raise(exception) if raiser.raise?
125
138
  # Raise the exception if this was the last attempt.
126
- @counter.raise_if_last_attempt(exception)
139
+ raise(exception) if @counter.last_attempt?
127
140
  # Sleep before the next attempt.
128
141
  @delayer.sleep
129
142
  # Try the block again.
130
143
  retry
131
144
  end
132
145
  end
146
+
147
+ # Raised when a block times out.
148
+ class TimeoutError < Timeout::Error; end
149
+
150
+ # Raised when a block returns an invalid result.
151
+ class InvalidResult < RuntimeError; end
133
152
  end #Wait
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wait
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: '0.4'
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: 2013-01-06 00:00:00.000000000 Z
12
+ date: 2013-01-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: todd@paperlesspost.com