Sha256: f0e3aed284465d6b8234e8e6577648d47be77917a5198df6dd083fe081d73c22

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'timeout'

module Timeouter
  class Timer

    attr_reader :started_at, :exhausted_at

    def initialize(timeout = 0, eclass: Timeouter::TimeoutError, emessage: 'execution expired')
      # ensure only positive timeouts
      timeout ||= 0
      timeout = [timeout, 0].max

      @eclass = eclass
      @emessage = emessage

      @started_at = Time.now
      @exhausted_at = timeout > 0 ? @started_at + timeout : nil
    end

    # elapsed time from creation
    def elapsed
      Time.now - @started_at
    end

    # time left to be exhausted or nil if timeout was 0
    def left
      @exhausted_at && [@exhausted_at - Time.now, 0].max
    end

    # is timeout exhausted? or nil when timeout was 0
    def exhausted?
      @exhausted_at && (@exhausted_at < Time.now)
    end

    # is timeout NOT exhausted? or true when timeout was 0
    def running?
      !exhausted?
    end

    # ensure timeout NOT exhausted raise exception otherwise
    def running!(eclass = @eclass, message: @emessage)
      !exhausted? || (raise eclass.new(message))
    end

    # run block in loop until timeout reached. Use break for returning result
    def loop
      yield(self) while self.running?
    end

    def loop!(eclass = @eclass, message: @emessage)
      yield(self) while self.running!(eclass, message: message)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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