Class: DDMetrics::Stopwatch

Inherits:
Object
  • Object
show all
Defined in:
lib/ddmetrics/stopwatch.rb

Defined Under Namespace

Classes: AlreadyRunningError, NotRunningError, StillRunningError

Instance Method Summary collapse

Constructor Details

#initializeStopwatch

Returns a new instance of Stopwatch.



23
24
25
26
# File 'lib/ddmetrics/stopwatch.rb', line 23

def initialize
  @duration = 0.0
  @last_start = nil
end

Instance Method Details

#durationObject

Raises:



39
40
41
42
# File 'lib/ddmetrics/stopwatch.rb', line 39

def duration
  raise StillRunningError if running?
  @duration
end

#running?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ddmetrics/stopwatch.rb', line 44

def running?
  !@last_start.nil?
end

#startObject



28
29
30
31
# File 'lib/ddmetrics/stopwatch.rb', line 28

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

#stopObject

Raises:



33
34
35
36
37
# File 'lib/ddmetrics/stopwatch.rb', line 33

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

#stopped?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ddmetrics/stopwatch.rb', line 48

def stopped?
  !running?
end