Sha256: 7c93ce2c93d46e950af21e4cc1661e1e84e99cdf9b5ea8499220a6f9fcfac0a9

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

module Timers
  # An individual timer set to fire a given proc at a given time
  class Timer
    include Comparable
    attr_reader :interval, :offset, :recurring

    def initialize(timers, interval, recurring = false, &block)
      @timers, @interval, @recurring = timers, interval, recurring
      @block  = block
      @offset = nil

      reset
    end

    def <=>(other)
      @offset <=> other.offset
    end

    # Cancel this timer
    def cancel
      @timers.cancel self
    end

    # Extend this timer
    def delay(seconds)
      @timers.delete self
      @offset += seconds
      @timers.add self
    end

    # Reset this timer
    def reset(offset = @timers.current_offset)
      @timers.cancel self if @offset
      @offset = Float(offset) + @interval
      @timers.add self
    end

    # Fire the block
    def fire(offset = @timers.current_offset)
      if recurring
        reset(offset)
      else
        @offset = offset
      end
      
      @block.call
    end
    alias_method :call, :fire

    # Pause this timer
    def pause
      @timers.pause self
    end

    # Continue this timer
    def continue
      @timers.continue self
    end

    # Number of seconds until next fire / since last fire
    def fires_in
      @offset - @timers.current_offset if @offset
    end

    # Inspect a timer
    def inspect
      str = "#<Timers::Timer:#{object_id.to_s(16)} "
      offset = @timers.current_offset

      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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
timers-3.0.1 lib/timers/timer.rb
timers-3.0.0 lib/timers/timer.rb
timers-3.0.0.pre lib/timers/timer.rb