class Timers::Timer

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

Attributes

interval[R]
recurring[R]
time[R]

Public Class Methods

new(timers, interval, recurring = false, &block) click to toggle source
# File lib/timers.rb, line 71
def initialize(timers, interval, recurring = false, &block)
  @timers, @interval, @recurring = timers, interval, recurring
  @block = block
  @time  = nil

  reset
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/timers.rb, line 79
def <=>(other)
  @time <=> other.time
end
call(now = Time.now)
Alias for: fire
cancel() click to toggle source

Cancel this timer

# File lib/timers.rb, line 84
def cancel
  @timers.cancel self
end
fire(now = Time.now) click to toggle source

Fire the block

# File lib/timers.rb, line 96
def fire(now = Time.now)
  reset(now) if recurring
  @block.call
end
Also aliased as: call
inspect() click to toggle source

Inspect a timer

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

  if @time
    if @time >= now
      str << "fires in #{@time - now} seconds"
    else
      str << "fired #{now - @time} seconds ago"
    end

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

  str << ">"
end
reset(now = Time.now) click to toggle source

Reset this timer

# File lib/timers.rb, line 89
def reset(now = Time.now)
  @timers.cancel self if @time
  @time = now + @interval
  @timers.add self
end