Sha256: 73b19e1fa0fe69f49300d9e6b6238ae3aedd7a69c0a50b2b8dfa3f8c1ecd6ecc

Contents?: true

Size: 1.02 KB

Versions: 35

Compression:

Stored size: 1.02 KB

Contents

require 'timeout'

module Cistern
  module WaitFor
    def timeout
      @timeout || Cistern.timeout
    end

    def timeout=(timeout)
      @timeout = timeout
    end

    def poll_interval
      @poll_interval || Cistern.poll_interval
    end

    def poll_interval=(poll_interval)
      @poll_interval = poll_interval
    end

    def timeout_error=(timeout_error)
      @timeout_error = timeout_error
    end

    def timeout_error
      @timeout_error || self.const_defined?(:Timeout) && self.const_get(:Timeout) || ::Timeout::Error
    end

    def wait_for(timeout = self.timeout, interval = self.poll_interval, &block)
      duration = 0
      start    = Time.now

      until yield || duration > timeout
        sleep(interval.to_f)
        duration = Time.now - start
      end

      duration > timeout ? false : duration
    end

    def wait_for!(timeout = self.timeout, interval = self.poll_interval, &block)
      wait_for(timeout, interval, &block) || raise(timeout_error, "wait_for(#{timeout}) exceeded")
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
cistern-0.12.3 lib/cistern/wait_for.rb
cistern-2.2.3 lib/cistern/wait_for.rb
cistern-2.2.1 lib/cistern/wait_for.rb
cistern-2.1.0 lib/cistern/wait_for.rb
cistern-2.0.5 lib/cistern/wait_for.rb
cistern-2.0.4 lib/cistern/wait_for.rb
cistern-0.12.2 lib/cistern/wait_for.rb
cistern-0.12.1 lib/cistern/wait_for.rb
cistern-2.0.3 lib/cistern/wait_for.rb
cistern-2.0.2 lib/cistern/wait_for.rb
cistern-0.11.3 lib/cistern/wait_for.rb
cistern-2.0.1 lib/cistern/wait_for.rb
cistern-1.0.1.pre6 lib/cistern/wait_for.rb
cistern-1.0.1.pre5 lib/cistern/wait_for.rb
cistern-1.0.1.pre4 lib/cistern/wait_for.rb
cistern-1.0.1.pre3 lib/cistern/wait_for.rb
cistern-1.0.1.pre2 lib/cistern/wait_for.rb
cistern-0.11.2 lib/cistern/wait_for.rb
cistern-1.0.1.pre1 lib/cistern/wait_for.rb
cistern-1.0.0.pre lib/cistern/wait_for.rb