Class: SpecForge::Forge::Timer
- Inherits:
-
Object
- Object
- SpecForge::Forge::Timer
- Defined in:
- lib/spec_forge/forge/timer.rb
Overview
Simple timer for tracking execution duration
Used to measure how long a forge run takes from start to finish.
Instance Attribute Summary collapse
-
#started_at ⇒ Time?
readonly
Time when the timer started.
-
#stopped_at ⇒ Time?
readonly
Time when the timer stopped.
Instance Method Summary collapse
-
#initialize ⇒ Timer
constructor
Creates a new timer in the reset state.
-
#reset ⇒ Timer
Resets the timer to its initial state.
-
#start ⇒ Timer
Starts the timer.
-
#started? ⇒ Boolean
Returns whether the timer has been started.
-
#stop ⇒ Timer
Stops the timer.
-
#stopped? ⇒ Boolean
Returns whether the timer has been stopped.
-
#time_elapsed ⇒ Float
Returns the elapsed time in seconds.
Constructor Details
#initialize ⇒ Timer
Creates a new timer in the reset state
22 23 24 |
# File 'lib/spec_forge/forge/timer.rb', line 22 def initialize reset end |
Instance Attribute Details
#started_at ⇒ Time? (readonly)
Returns Time when the timer started.
12 13 14 |
# File 'lib/spec_forge/forge/timer.rb', line 12 def started_at @started_at end |
#stopped_at ⇒ Time? (readonly)
Returns Time when the timer stopped.
15 16 17 |
# File 'lib/spec_forge/forge/timer.rb', line 15 def stopped_at @stopped_at end |
Instance Method Details
#reset ⇒ Timer
Resets the timer to its initial state
31 32 33 34 35 36 |
# File 'lib/spec_forge/forge/timer.rb', line 31 def reset @started_at = nil @stopped_at = nil self end |
#start ⇒ Timer
Starts the timer
43 44 45 46 47 48 49 |
# File 'lib/spec_forge/forge/timer.rb', line 43 def start reset @started_at ||= Time.current self end |
#started? ⇒ Boolean
Returns whether the timer has been started
69 70 71 |
# File 'lib/spec_forge/forge/timer.rb', line 69 def started? !started_at.nil? end |
#stop ⇒ Timer
Stops the timer
56 57 58 59 60 61 62 |
# File 'lib/spec_forge/forge/timer.rb', line 56 def stop return self if @started_at.nil? @stopped_at ||= Time.current self end |
#stopped? ⇒ Boolean
Returns whether the timer has been stopped
78 79 80 |
# File 'lib/spec_forge/forge/timer.rb', line 78 def stopped? !stopped_at.nil? end |
#time_elapsed ⇒ Float
Returns the elapsed time in seconds
87 88 89 90 91 |
# File 'lib/spec_forge/forge/timer.rb', line 87 def time_elapsed return 0 if started_at.nil? (stopped_at || Time.current) - started_at end |