Sha256: 0e741fa71b7106dd97df8ccf45660bdbb6afeb94bed406cdef1c60b2bc92536f

Contents?: true

Size: 635 Bytes

Versions: 1

Compression:

Stored size: 635 Bytes

Contents

module Pagetience
  class Timer
    attr_accessor :timeout, :polling, :block

    def initialize(timeout=30, polling=1, &block)
      @timeout = timeout
      @polling = polling
      @block = block
    end

    def run_until(expected=nil)
      raise ArgumentError, 'Timeout cannot be lower than the polling value.' unless @timeout > @polling

      while @timeout > 0 && @timeout > @polling
        @latest_result = @block.call
        break unless expected && @latest_result != expected
        sleep @polling
        @timeout = @timeout - @polling
      end

      @latest_result
    end
    alias_method :run, :run_until
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pagetience-0.1.0 lib/pagetience/timer.rb