Sha256: 5026d8ecca77f30a5fe0f529ca81ee673073b48318087e648b629af199fc7a55

Contents?: true

Size: 784 Bytes

Versions: 20

Compression:

Stored size: 784 Bytes

Contents

# frozen_string_literal: true

module Nanoc::Telemetry
  class Stopwatch
    attr_reader :duration

    class AlreadyRunningError < StandardError
      def message
        'Cannot start, because stopwatch is already running'
      end
    end

    class NotRunningError < StandardError
      def message
        'Cannot stop, because stopwatch is not running'
      end
    end

    def initialize
      @duration = 0.0
      @last_start = nil
    end

    def start
      raise AlreadyRunningError if running?
      @last_start = Time.now
    end

    def stop
      raise NotRunningError unless running?
      @duration += (Time.now - @last_start)
      @last_start = nil
    end

    def running?
      !@last_start.nil?
    end

    def stopped?
      !running?
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
nanoc-4.8.14 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.13 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.12 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.11 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.10 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.9 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.8 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.7 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.6 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.5 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.4 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.3 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.2 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.1 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.8.0 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.7.14 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.7.13 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.7.12 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.7.11 lib/nanoc/telemetry/stopwatch.rb
nanoc-4.7.10 lib/nanoc/telemetry/stopwatch.rb