Class: Charming::Components::Stopwatch

Inherits:
Charming::Component show all
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

Instance Method Summary collapse

Methods inherited from Charming::Component

#captures_text?

Methods inherited from View

#focused?, #layout_assigns

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

#elapsedObject (readonly)

Returns the value of attribute elapsed.



8
9
10
# File 'lib/charming/presentation/components/stopwatch.rb', line 8

def elapsed
  @elapsed
end

#labelObject (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

#renderObject

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

#resetObject

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.

Returns:

  • (Boolean)


31
32
33
# File 'lib/charming/presentation/components/stopwatch.rb', line 31

def running?
  @running
end

#startObject

Starts accumulating time. Returns self.



19
20
21
22
# File 'lib/charming/presentation/components/stopwatch.rb', line 19

def start
  @running = true
  self
end

#stopObject

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