Class: Charming::Components::Stopwatch
- Inherits:
-
Charming::Component
- Object
- View
- Charming::Component
- Charming::Components::Stopwatch
- Defined in:
- lib/charming/presentation/components/stopwatch.rb
Overview
Stopwatch is a count-up display. Drive it from a controller timer: call
tick per interval; elapsed time accumulates only while running.
Instance Attribute Summary collapse
-
#elapsed ⇒ Object
readonly
Returns the value of attribute elapsed.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
Instance Method Summary collapse
-
#initialize(label: nil, theme: nil) ⇒ Stopwatch
constructor
label is an optional suffix shown after the time.
-
#render ⇒ Object
Renders the elapsed time (with the label appended when present).
-
#reset ⇒ Object
Stops and zeroes the elapsed time.
-
#running? ⇒ Boolean
True while the stopwatch is accumulating time.
-
#start ⇒ Object
Starts accumulating time.
-
#stop ⇒ Object
Pauses accumulation.
-
#tick(seconds = 1) ⇒ Object
Adds seconds (default 1) when running; a no-op otherwise.
Methods inherited from Charming::Component
Methods inherited from View
Constructor Details
#initialize(label: nil, theme: nil) ⇒ Stopwatch
label is an optional suffix shown after the time.
11 12 13 14 15 16 |
# File 'lib/charming/presentation/components/stopwatch.rb', line 11 def initialize(label: nil, theme: nil) super(theme: theme) @elapsed = 0 @running = false @label = label end |
Instance Attribute Details
#elapsed ⇒ Object (readonly)
Returns the value of attribute elapsed.
8 9 10 |
# File 'lib/charming/presentation/components/stopwatch.rb', line 8 def elapsed @elapsed end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
8 9 10 |
# File 'lib/charming/presentation/components/stopwatch.rb', line 8 def label @label end |
Instance Method Details
#render ⇒ Object
Renders the elapsed time (with the label appended when present).
49 50 51 52 |
# File 'lib/charming/presentation/components/stopwatch.rb', line 49 def render clock = TimeDisplay.clock(elapsed) label ? "#{clock} #{label}" : clock end |
#reset ⇒ Object
Stops and zeroes the elapsed time. Returns self.
42 43 44 45 46 |
# File 'lib/charming/presentation/components/stopwatch.rb', line 42 def reset @elapsed = 0 @running = false self end |
#running? ⇒ Boolean
True while the stopwatch is accumulating time.
31 32 33 |
# File 'lib/charming/presentation/components/stopwatch.rb', line 31 def running? @running end |
#start ⇒ Object
Starts accumulating time. Returns self.
19 20 21 22 |
# File 'lib/charming/presentation/components/stopwatch.rb', line 19 def start @running = true self end |
#stop ⇒ Object
Pauses accumulation. Returns self.
25 26 27 28 |
# File 'lib/charming/presentation/components/stopwatch.rb', line 25 def stop @running = false self end |
#tick(seconds = 1) ⇒ Object
Adds seconds (default 1) when running; a no-op otherwise. Returns self.
36 37 38 39 |
# File 'lib/charming/presentation/components/stopwatch.rb', line 36 def tick(seconds = 1) @elapsed += seconds if running? self end |