Class: Timers::Wait

Inherits:
Object
  • Object
show all
Defined in:
lib/timers/wait.rb

Overview

An exclusive, monotonic timeout class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration) ⇒ Wait

Returns a new instance of Wait.



26
27
28
29
# File 'lib/timers/wait.rb', line 26

def initialize(duration)
	@duration = duration
	@remaining = true
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



31
32
33
# File 'lib/timers/wait.rb', line 31

def duration
  @duration
end

#remainingObject (readonly)

Returns the value of attribute remaining.



32
33
34
# File 'lib/timers/wait.rb', line 32

def remaining
  @remaining
end

Class Method Details

.for(duration, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/timers/wait.rb', line 14

def self.for(duration, &block)
	if duration
		timeout = new(duration)
		
		timeout.while_time_remaining(&block)
	else
		loop do
			yield(nil)
		end
	end
end

Instance Method Details

#while_time_remainingObject

Yields while time remains for work to be done:



35
36
37
38
39
40
41
42
43
# File 'lib/timers/wait.rb', line 35

def while_time_remaining
	@interval = Interval.new
	@interval.start
	
	yield @remaining while time_remaining?
ensure
	@interval.stop
	@interval = nil
end