An individual timer set to fire a given proc at a given time
# 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
# File lib/timers.rb, line 79 def <=>(other) @time <=> other.time end
Cancel this timer
# File lib/timers.rb, line 84 def cancel @timers.cancel self end
Fire the block
# File lib/timers.rb, line 96 def fire(now = Time.now) reset(now) if recurring @block.call end
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 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