Class: Charming::Components::Spinner

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

Constant Summary collapse

DEFAULT_FRAMES =
["-", "\\", "|", "/"].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

Returns a new instance of Spinner.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
# File 'lib/charming/components/spinner.rb', line 10

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)

Returns the value of attribute frames.



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

def frames
  @frames
end

#indexObject (readonly)

Returns the value of attribute index.



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

def index
  @index
end

#labelObject (readonly)

Returns the value of attribute label.



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

def label
  @label
end

Instance Method Details

#renderObject



24
25
26
27
28
# File 'lib/charming/components/spinner.rb', line 24

def render
  return frame unless label

  "#{frame} #{label}"
end

#tickObject



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

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