Class: DDMetrics::Stopwatch
- Inherits:
-
Object
- Object
- DDMetrics::Stopwatch
show all
- Defined in:
- lib/ddmetrics/stopwatch.rb
Defined Under Namespace
Classes: AlreadyRunningError, NotRunningError, StillRunningError
Instance Method Summary
collapse
Constructor Details
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
#duration ⇒ Object
39
40
41
42
|
# File 'lib/ddmetrics/stopwatch.rb', line 39
def duration
raise StillRunningError if running?
@duration
end
|
#running? ⇒ Boolean
44
45
46
|
# File 'lib/ddmetrics/stopwatch.rb', line 44
def running?
!@last_start.nil?
end
|
#start ⇒ Object
28
29
30
31
|
# File 'lib/ddmetrics/stopwatch.rb', line 28
def start
raise AlreadyRunningError if running?
@last_start = Time.now
end
|
#stop ⇒ Object
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
48
49
50
|
# File 'lib/ddmetrics/stopwatch.rb', line 48
def stopped?
!running?
end
|