Class: Charming::Presentation::Components::Spinner
- Inherits:
-
Charming::Presentation::Component
- Object
- View
- Charming::Presentation::Component
- Charming::Presentation::Components::Spinner
- 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
-
#frames ⇒ Object
readonly
The current frame list, frame index, and optional label string.
-
#index ⇒ Object
readonly
The current frame list, frame index, and optional label string.
-
#label ⇒ Object
readonly
The current frame list, frame index, and optional label string.
Instance Method Summary collapse
-
#initialize(frames: DEFAULT_FRAMES, index: 0, label: nil) ⇒ Spinner
constructor
frames defaults to DEFAULT_FRAMES but may be replaced with any array of frame strings.
-
#render ⇒ Object
Renders the current frame, optionally followed by the label and a space.
-
#tick ⇒ Object
Advances the frame index by one position, wrapping around.
Methods inherited from View
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.
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
#frames ⇒ Object (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 |
#index ⇒ Object (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 |
#label ⇒ Object (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
#render ⇒ Object
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 |
#tick ⇒ Object
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 |