Class: Timer

Class | Timer
In: | lib/more/facets/timer.rb
Parent: | Object

# Timer

Provides a strightforward means for controlling time critical execution. Can be used as a "stop watch" timer or as a "time bomb" timer.

      t = Timer.new(10) { raise TimeoutError, "timeout!" }
      t.start
        :      # done within 10sec timeout
      t.stop
      t.start
        :
      if condition then
        t.reset       #--> restart timer
      end

A Kernel method is also provided for easily timing the exectuion of a block.

      timed { |timer|
         timer.total_time.round #=> 0

         sleep 1
         timer.total_time.round #=> 1

         timer.stop
         timer.total_time.round #=> 1

         sleep 2
         timer.total_time.round #=> 1

         timer.start
         timer.total_time.round #=> 1

         sleep 1
         timer.total_time.round #=> 2
      }

### Methods

defuse limit new on_timeout reset reset_limit running? start stop stopped? total_time

### Classes and Modules

Class Timer::Dummy

### Attributes

end_time | [R] |
start_time | [R] |
time_limit | [RW] |

### Public Class methods

new( time_limit=nil, &block )

[Source]

    # File lib/more/facets/timer.rb, line 110
      def initialize( time_limit=nil, &block )
        # standard timer
        @start_time = nil
        @end_time = nil
        @total_time = 0
        @runnning = nil
        # for using time limit
        @time_limit = time_limit
        @on_timeout = block
        @current_thread = nil
        @timer_thread = nil
      end

### Public Instance methods

defuse()

Kill time limit thread, if any.

[Source]

    # File lib/more/facets/timer.rb, line 171
      def defuse
        if @timer_thread
          Thread.kill @timer_thread
          @timer_thread = nil
        end
      end

limit( time_limit=nil )

Establish a time limit on execution.

[Source]

    # File lib/more/facets/timer.rb, line 155
      def limit( time_limit=nil )
        if @time_limit || time_limit
          @current_thread = Thread.current
          @timer_thread = Thread.fork {
            sleep @time_limit
            if @on_timeout then
              @on_timeout.call @time_limit
            else
              @current_thread.raise TimeoutError, "#{@time_limit} seconds past"
            end
          }
        end
      end

on_timeout( &block )

[Source]

    # File lib/more/facets/timer.rb, line 123
      def on_timeout( &block )
        if block then
          @on_timeout = block
          true
        else
          false
        end
      end

reset()

Stops and resets the timer. If the timer was running returns the total time. If not returns 0.

[Source]

    # File lib/more/facets/timer.rb, line 196
      def reset
        if running?
          r = stop
        else
          r = 0
        end
        @total_time = 0
        return r
      end

reset_limit()

Resets the time limit. Same as:

      t.stop
      t.start

[Source]

    # File lib/more/facets/timer.rb, line 212
      def reset_limit
        #stop
        #start
        defuse
        limit
      end

running?()

Queries whether the timer is still running.

[Source]

    # File lib/more/facets/timer.rb, line 221
      def running?
        return @running
      end

start()

Start the timer.

[Source]

    # File lib/more/facets/timer.rb, line 134
      def start
        @running = true
        @start_time = Time.now

        limit if @time_limit

        self

        #if block_given? then
        #  begin
        #    yield( self )
        #  ensure
        #    stop
        #  end
        #else
        #  @time_limit
        #end
      end

stop()

Stops timer and returns total time. If timer was not running returns false.

[Source]

    # File lib/more/facets/timer.rb, line 181
      def stop
        if @running
          defuse
          # record running time
          @end_time = Time.now
          @running = false
          @total_time += (@end_time - @start_time)
        else
          nil
        end
      end

stopped?()

Queries whether the timer is still not running.

[Source]

    # File lib/more/facets/timer.rb, line 227
      def stopped?
        return !@running
      end

total_time()

Queries total recorded time of timer.

[Source]

    # File lib/more/facets/timer.rb, line 233
      def total_time
        if running? then
          return @total_time + (Time.now - @start_time)
        else
          return @total_time
        end
      end

[Validate]