Class: Iodine::TimedEvent

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

Overview

Every timed event is a member of the TimedEvent class and responds to it’s methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reactor, interval, repeat_limit = -1,, args = [], job = nil) ⇒ TimedEvent

Initialize a timed event.



19
20
21
22
23
24
25
26
27
# File 'lib/iodine/timers.rb', line 19

def initialize reactor, interval, repeat_limit = -1, args=[], job=nil
  @reactor = reactor
  @interval = interval
  @repeat_limit = repeat_limit ? repeat_limit.to_i : -1
  @job = job || (Proc.new { stop! })
  @next = @reactor.time + interval
  args << self
  @args = args
end

Instance Attribute Details

#intervalObject

Sets/gets how often a timed event repeats, in seconds.



11
12
13
# File 'lib/iodine/timers.rb', line 11

def interval
  @interval
end

#jobObject

Allows you to acess or change the timer’s Proc object.



16
17
18
# File 'lib/iodine/timers.rb', line 16

def job
  @job
end

#repeat_limitObject

Sets/gets how many times a timed event repeats. If set to false or -1, the timed event will repead until the application quits.



14
15
16
# File 'lib/iodine/timers.rb', line 14

def repeat_limit
  @repeat_limit
end

Instance Method Details

#done?true, false

Returns true if the timer is finished.

If the timed event is due, this method will also add the event to the queue.

Returns:

  • (true, false)


41
42
43
44
45
46
47
48
# File 'lib/iodine/timers.rb', line 41

def done?
  return false unless @next <= @reactor.time
  return true if @repeat_limit == 0
  @repeat_limit -= 1 if @repeat_limit.to_i > 0
  @reactor.run(*@args, &@job)
  @next = @reactor.time + @interval
  @repeat_limit == 0
end

#stop!Iodine::TimedEvent

stops a timed event.

Returns:



31
32
33
34
35
# File 'lib/iodine/timers.rb', line 31

def stop!
  @repeat_limit = 0
  @next = @reactor.time
  self
end