Class: Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/zmb/timer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate, symbol, interval, repeat = false, data = nil) ⇒ Timer

interval is in seconds (decimals accepted)



6
7
8
9
10
11
12
13
14
# File 'lib/zmb/timer.rb', line 6

def initialize(delegate, symbol, interval, repeat=false, data=nil) # interval is in seconds (decimals accepted)
  @delegate = delegate
  @symbol = symbol
  @interval = interval
  @repeat = repeat
  @data = data
  
  @fire_at = Time.now + interval
end

Instance Attribute Details

#delegateObject

Returns the value of attribute delegate.



4
5
6
# File 'lib/zmb/timer.rb', line 4

def delegate
  @delegate
end

#symbolObject

Returns the value of attribute symbol.



4
5
6
# File 'lib/zmb/timer.rb', line 4

def symbol
  @symbol
end

Instance Method Details

#fire(sender) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/zmb/timer.rb', line 16

def fire(sender)
  begin
    if @data then
      @delegate.send @symbol, @data
    else
      @delegate.send @symbol
    end
  rescue Exception
    sender.debug(@delegate, "Timer #{@symbol} failed", $!)
  end

  if not @repeat
    sender.timer_delete(self) if sender.respond_to?('timer_delete')
  else
    @fire_at = Time.now + @interval
  end
end

#timeoutObject



34
35
36
# File 'lib/zmb/timer.rb', line 34

def timeout
  @fire_at - Time.now
end