lib/ddtelemetry/stopwatch.rb in ddtelemetry-1.0.0a1 vs lib/ddtelemetry/stopwatch.rb in ddtelemetry-1.0.0a2
- old
+ new
@@ -1,11 +1,9 @@
# frozen_string_literal: true
module DDTelemetry
class Stopwatch
- attr_reader :duration
-
class AlreadyRunningError < StandardError
def message
'Cannot start, because stopwatch is already running'
end
end
@@ -14,10 +12,16 @@
def message
'Cannot stop, because stopwatch is not running'
end
end
+ class StillRunningError < StandardError
+ def message
+ 'Cannot get duration, because stopwatch is still running'
+ end
+ end
+
def initialize
@duration = 0.0
@last_start = nil
end
@@ -28,9 +32,14 @@
def stop
raise NotRunningError unless running?
@duration += (Time.now - @last_start)
@last_start = nil
+ end
+
+ def duration
+ raise StillRunningError if running?
+ @duration
end
def running?
!@last_start.nil?
end