class Duration

Create a Duration at the start of the process and use it to sleep for delays

Attributes

mode[R]

Public Class Methods

new(duration, count, mode) click to toggle source

@param duration the total duration @param count the number of evenly-spaced delays within the duration @param mode only delay for this mode

# File lib/utils/duration.rb, line 23
def initialize(duration, count, mode)
  @delay = count > 0 ? duration / count : 0     # Time for each delay
  @deadline = Time.now + @delay     # End time of next call to #delay
  @mode = mode
end

Public Instance Methods

delay(mode) { |d| ... } click to toggle source

Return nil if mode != self.mode. Return the next delay period, taking account of cumulative time taken so far. If block given, call with the delay period.

# File lib/utils/duration.rb, line 34
def delay(mode)
  if mode == @mode
    d = @deadline - Time.now
    d = 0 if d < 0            # No negative delays
    @deadline += @delay
    block_given? ? yield(d) : d
  end
end
zero?() click to toggle source
# File lib/utils/duration.rb, line 43
def zero?() @delay.zero?; end