Class: Wait::BaseCounter
- Inherits:
-
Object
- Object
- Wait::BaseCounter
- Defined in:
- lib/counters/base.rb
Instance Attribute Summary collapse
-
#attempt ⇒ Object
readonly
Returns the value of attribute attempt.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
-
#increment ⇒ Object
Called before each attempt to increment the counter.
-
#initialize(total) ⇒ BaseCounter
constructor
A new instance of BaseCounter.
-
#last_attempt? ⇒ Boolean
Returns
true
if this is the last attempt. -
#log ⇒ Object
Logs the current attempt count.
-
#reset ⇒ Object
Called before all attempts to reset the counter.
-
#to_s ⇒ Object
Returns a string representation of the current count.
Constructor Details
#initialize(total) ⇒ BaseCounter
Returns a new instance of BaseCounter.
6 7 8 9 10 11 12 13 14 |
# File 'lib/counters/base.rb', line 6 def initialize(total) # Attempt to prevent causing an infinite loop by being very strict about # the value passed. unless total.is_a?(Integer) and total > 0 raise(ArgumentError, "invalid number of attempts: #{total.inspect}") end @total = total end |
Instance Attribute Details
#attempt ⇒ Object (readonly)
Returns the value of attribute attempt.
4 5 6 |
# File 'lib/counters/base.rb', line 4 def attempt @attempt end |
#logger ⇒ Object
Returns the value of attribute logger.
3 4 5 |
# File 'lib/counters/base.rb', line 3 def logger @logger end |
Instance Method Details
#increment ⇒ Object
Called before each attempt to increment the counter.
22 23 24 25 |
# File 'lib/counters/base.rb', line 22 def increment @attempt += 1 log end |
#last_attempt? ⇒ Boolean
Returns true
if this is the last attempt.
28 29 30 |
# File 'lib/counters/base.rb', line 28 def last_attempt? @attempt == @total end |
#log ⇒ Object
Logs the current attempt count.
33 34 35 36 37 |
# File 'lib/counters/base.rb', line 33 def log return if @logger.nil? @logger.debug("Counter") { "attempt #{self}" } end |
#reset ⇒ Object
Called before all attempts to reset the counter.
17 18 19 |
# File 'lib/counters/base.rb', line 17 def reset @attempt = 0 end |
#to_s ⇒ Object
Returns a string representation of the current count.
40 41 42 |
# File 'lib/counters/base.rb', line 40 def to_s [@attempt, @total].join("/") end |