Class: Charming::Components::Timer
- Inherits:
-
Charming::Component
- Object
- View
- Charming::Component
- Charming::Components::Timer
- Defined in:
- lib/charming/presentation/components/timer.rb
Overview
Timer is a countdown display. Drive it from a controller timer: call
tick per interval and check expired? to act when time runs out.
Instance Attribute Summary collapse
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#remaining ⇒ Object
readonly
Returns the value of attribute remaining.
Instance Method Summary collapse
-
#expired? ⇒ Boolean
True once the countdown has reached zero.
-
#initialize(duration:, label: nil, theme: nil) ⇒ Timer
constructor
duration is the countdown length in seconds.
-
#render ⇒ Object
Renders the remaining time (with the label appended when present).
-
#reset ⇒ Object
Restores the full duration.
-
#tick(seconds = 1) ⇒ Object
Counts down by seconds (default 1), clamping at zero.
Methods inherited from Charming::Component
Methods inherited from View
Constructor Details
#initialize(duration:, label: nil, theme: nil) ⇒ Timer
duration is the countdown length in seconds. label is an optional suffix shown after the time.
12 13 14 15 16 17 |
# File 'lib/charming/presentation/components/timer.rb', line 12 def initialize(duration:, label: nil, theme: nil) super(theme: theme) @duration = [duration.to_i, 0].max @remaining = @duration @label = label end |
Instance Attribute Details
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
8 9 10 |
# File 'lib/charming/presentation/components/timer.rb', line 8 def duration @duration end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
8 9 10 |
# File 'lib/charming/presentation/components/timer.rb', line 8 def label @label end |
#remaining ⇒ Object (readonly)
Returns the value of attribute remaining.
8 9 10 |
# File 'lib/charming/presentation/components/timer.rb', line 8 def remaining @remaining end |
Instance Method Details
#expired? ⇒ Boolean
True once the countdown has reached zero.
26 27 28 |
# File 'lib/charming/presentation/components/timer.rb', line 26 def expired? remaining.zero? end |
#render ⇒ Object
Renders the remaining time (with the label appended when present).
37 38 39 40 |
# File 'lib/charming/presentation/components/timer.rb', line 37 def render clock = TimeDisplay.clock(remaining) label ? "#{clock} #{label}" : clock end |
#reset ⇒ Object
Restores the full duration. Returns self.
31 32 33 34 |
# File 'lib/charming/presentation/components/timer.rb', line 31 def reset @remaining = duration self end |
#tick(seconds = 1) ⇒ Object
Counts down by seconds (default 1), clamping at zero. Returns self.
20 21 22 23 |
# File 'lib/charming/presentation/components/timer.rb', line 20 def tick(seconds = 1) @remaining = [@remaining - seconds.to_i, 0].max self end |