Class: Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/duration.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration, count, mode) ⇒ Duration

Returns a new instance of Duration.

Parameters:

  • duration

    the total duration

  • count

    the number of evenly-spaced delays within the duration

  • mode

    only delay for this mode



23
24
25
26
27
# 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

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



29
30
31
# File 'lib/utils/duration.rb', line 29

def mode
  @mode
end

Instance Method Details

#delay(mode) ⇒ Object

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.



34
35
36
37
38
39
40
41
# 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?Boolean

Returns:

  • (Boolean)


43
# File 'lib/utils/duration.rb', line 43

def zero?() @delay.zero?; end