Parent

Included Modules

Class/Module Index [+]

Quicksearch

Timers::Timer

An individual timer set to fire a given proc at a given time

Attributes

interval[R]
offset[R]
recurring[R]

Public Class Methods

new(group, interval, recurring = false, offset = nil, &block) click to toggle source
# File lib/timers/timer.rb, line 8
def initialize(group, interval, recurring = false, offset = nil, &block)
  @group = group
  
  @group.timers << self
  
  @interval = interval
  @recurring = recurring
  @block = block
  @offset = offset
  
  @handle = nil
  
  # If a start offset was supplied, use that, otherwise use the current timers offset.
  reset(@offset || @group.current_offset)
end

Public Instance Methods

call(offset = @group.current_offset) click to toggle source
Alias for: fire
cancel() click to toggle source

Cancel this timer

# File lib/timers/timer.rb, line 59
def cancel
  @handle.cancel! if @handle
  @handle = nil
  
  # This timer is no longer valid:
  @group.timers.delete self
  @group = nil
end
continue() click to toggle source
Alias for: resume
delay(seconds) click to toggle source

Extend this timer

# File lib/timers/timer.rb, line 50
def delay(seconds)
  @handle.cancel! if @handle
  
  @offset += seconds
  
  @handle = @group.events.schedule(@offset, self)
end
fire(offset = @group.current_offset) click to toggle source

Fire the block

# File lib/timers/timer.rb, line 78
def fire(offset = @group.current_offset)
  if recurring == :strict
    # ... make the next interval strictly the last offset + the interval:
    reset(@offset)
  elsif recurring
    reset(offset)
  else
    @offset = offset
  end

  @block.call(offset)
end
Also aliased as: call
fires_in() click to toggle source

Number of seconds until next fire / since last fire

# File lib/timers/timer.rb, line 94
def fires_in
  @offset - @group.current_offset if @offset
end
inspect() click to toggle source

Inspect a timer

# File lib/timers/timer.rb, line 99
def inspect
  str = "#<Timers::Timer:#{object_id.to_s(16)} "

  if @offset
    if fires_in >= 0
      str << "fires in #{fires_in} seconds"
    else
      str << "fired #{fires_in.abs} seconds ago"
    end

    str << ", recurs every #{interval}" if recurring
  else
    str << "dead"
  end

  str << ">"
end
pause() click to toggle source
# File lib/timers/timer.rb, line 28
def pause
  return if paused?
  
  @group.timers.delete self
  @group.paused_timers.add self
  
  @handle.cancel! if @handle
  @handle = nil
end
paused?() click to toggle source
# File lib/timers/timer.rb, line 24
def paused?
  @group.paused_timers.include? self
end
reset(offset = @group.current_offset) click to toggle source

Reset this timer

# File lib/timers/timer.rb, line 69
def reset(offset = @group.current_offset)
  @handle.cancel! if @handle
  
  @offset = Float(offset) + @interval
  
  @handle = @group.events.schedule(@offset, self)
end
resume() click to toggle source
# File lib/timers/timer.rb, line 38
def resume
  return unless paused?
  
  @group.timers.add self
  @group.paused_timers.delete self
  
  reset
end
Also aliased as: continue

[Validate]

Generated with the Darkfish Rdoc Generator 2.