Class: Presently::Clock
- Inherits:
-
Object
- Object
- Presently::Clock
- Defined in:
- lib/presently/clock.rb
Overview
A simple clock that tracks elapsed time with start, pause, resume, and reset.
The clock accumulates elapsed time while running and freezes it when paused.
Instance Method Summary collapse
-
#elapsed ⇒ Object
The total elapsed time in seconds.
-
#initialize ⇒ Clock
constructor
Initialize a new clock in the stopped state.
-
#pause! ⇒ Object
Pause the clock.
-
#paused? ⇒ Boolean
Whether the clock has been started but is currently paused.
-
#reset!(elapsed = 0) ⇒ Object
Reset the elapsed time to the given value.
-
#restore!(elapsed, running:) ⇒ Object
Directly restore the clock to a previously persisted state.
-
#resume! ⇒ Object
Resume the clock after a pause.
-
#running? ⇒ Boolean
Whether the clock is currently running and accumulating time.
-
#start! ⇒ Object
Start the clock.
-
#started? ⇒ Boolean
Whether the clock has been started at least once.
Constructor Details
#initialize ⇒ Clock
Initialize a new clock in the stopped state.
12 13 14 15 16 17 |
# File 'lib/presently/clock.rb', line 12 def initialize @elapsed = 0 @started = false @running = false @last_tick = nil end |
Instance Method Details
#elapsed ⇒ Object
The total elapsed time in seconds. Includes time accumulated up to now if running, or frozen time if paused.
40 41 42 43 44 45 46 |
# File 'lib/presently/clock.rb', line 40 def elapsed if @running @elapsed + (Time.now - @last_tick) else @elapsed end end |
#pause! ⇒ Object
Pause the clock. Freezes the elapsed time at the current value.
66 67 68 69 70 71 |
# File 'lib/presently/clock.rb', line 66 def pause! return unless @running @elapsed += Time.now - @last_tick @running = false end |
#paused? ⇒ Boolean
Whether the clock has been started but is currently paused.
33 34 35 |
# File 'lib/presently/clock.rb', line 33 def paused? started? && !@running end |
#reset!(elapsed = 0) ⇒ Object
Reset the elapsed time to the given value. If running, continues from the new value. If paused, sets the frozen value.
84 85 86 87 |
# File 'lib/presently/clock.rb', line 84 def reset!(elapsed = 0) @elapsed = elapsed @last_tick = Time.now if @running end |
#restore!(elapsed, running:) ⇒ Object
Directly restore the clock to a previously persisted state.
58 59 60 61 62 63 |
# File 'lib/presently/clock.rb', line 58 def restore!(elapsed, running:) @started = true @elapsed = elapsed @running = running @last_tick = running ? Time.now : nil end |
#resume! ⇒ Object
Resume the clock after a pause. Continues accumulating time from now.
74 75 76 77 78 79 |
# File 'lib/presently/clock.rb', line 74 def resume! return if @running @running = true @last_tick = Time.now end |
#running? ⇒ Boolean
Whether the clock is currently running and accumulating time.
27 28 29 |
# File 'lib/presently/clock.rb', line 27 def running? @running end |
#start! ⇒ Object
Start the clock. Begins accumulating time from now.
49 50 51 52 53 |
# File 'lib/presently/clock.rb', line 49 def start! @started = true @running = true @last_tick = Time.now end |
#started? ⇒ Boolean
Whether the clock has been started at least once.
21 22 23 |
# File 'lib/presently/clock.rb', line 21 def started? @started end |