Class: Wait::BaseCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/counters/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attemptObject (readonly)

Returns the value of attribute attempt.



4
5
6
# File 'lib/counters/base.rb', line 4

def attempt
  @attempt
end

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/counters/base.rb', line 3

def logger
  @logger
end

Instance Method Details

#incrementObject

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.

Returns:

  • (Boolean)


28
29
30
# File 'lib/counters/base.rb', line 28

def last_attempt?
  @attempt == @total
end

#logObject

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

#resetObject

Called before all attempts to reset the counter.



17
18
19
# File 'lib/counters/base.rb', line 17

def reset
  @attempt = 0
end

#to_sObject

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