Class: Charming::Presentation::Components::Spinner

Inherits:
Charming::Presentation::Component show all
Defined in:
lib/charming/presentation/components/spinner.rb

Overview

Spinner is a simple rotating-frame indicator. The component cycles through a list of frames on each ‘tick`; pair it with a controller timer to drive animation. An optional label is appended after the current frame on each render.

Constant Summary collapse

DEFAULT_FRAMES =

The default frame set: a 4-frame ASCII spinner.

["-", "\\", "|", "/"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(frames: DEFAULT_FRAMES, index: 0, label: nil) ⇒ Spinner

frames defaults to DEFAULT_FRAMES but may be replaced with any array of frame strings. index is the starting frame index. label is an optional suffix shown after the frame.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
# File 'lib/charming/presentation/components/spinner.rb', line 18

def initialize(frames: DEFAULT_FRAMES, index: 0, label: nil)
  super()
  raise ArgumentError, "frames cannot be empty" if frames.empty?

  @frames = frames
  @index = index
  @label = label
end

Instance Attribute Details

#framesObject (readonly)

The current frame list, frame index, and optional label string.



14
15
16
# File 'lib/charming/presentation/components/spinner.rb', line 14

def frames
  @frames
end

#indexObject (readonly)

The current frame list, frame index, and optional label string.



14
15
16
# File 'lib/charming/presentation/components/spinner.rb', line 14

def index
  @index
end

#labelObject (readonly)

The current frame list, frame index, and optional label string.



14
15
16
# File 'lib/charming/presentation/components/spinner.rb', line 14

def label
  @label
end

Instance Method Details

#renderObject

Renders the current frame, optionally followed by the label and a space.



34
35
36
37
38
# File 'lib/charming/presentation/components/spinner.rb', line 34

def render
  return frame unless label

  "#{frame} #{label}"
end

#tickObject

Advances the frame index by one position, wrapping around. Returns self for chaining.



28
29
30
31
# File 'lib/charming/presentation/components/spinner.rb', line 28

def tick
  @index = (index + 1) % frames.length
  self
end