Class: Duration
- Inherits:
-
Object
- Object
- Duration
- 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
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
-
#delay(mode) ⇒ Object
Return nil if mode != self.mode.
-
#initialize(duration, count, mode) ⇒ Duration
constructor
A new instance of Duration.
- #zero? ⇒ Boolean
Constructor Details
#initialize(duration, count, mode) ⇒ Duration
Returns a new instance of Duration.
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
#mode ⇒ Object (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
43 |
# File 'lib/utils/duration.rb', line 43 def zero?() @delay.zero?; end |