Class: Charming::Components::Progressbar
- Inherits:
-
Charming::Component
- Object
- View
- Charming::Component
- Charming::Components::Progressbar
- Defined in:
- lib/charming/components/progressbar.rb
Instance Attribute Summary collapse
-
#bar_format ⇒ Object
Returns the value of attribute bar_format.
-
#complete ⇒ Object
Returns the value of attribute complete.
-
#current ⇒ Object
Returns the value of attribute current.
-
#incomplete ⇒ Object
Returns the value of attribute incomplete.
-
#label ⇒ Object
Returns the value of attribute label.
-
#total ⇒ Object
Returns the value of attribute total.
Instance Method Summary collapse
- #complete! ⇒ Object
-
#initialize(total:, complete: "=", incomplete: " ", bar_format: :classic, label: nil) ⇒ Progressbar
constructor
A new instance of Progressbar.
- #render ⇒ Object
- #tick(count = 1) ⇒ Object
- #update(value) ⇒ Object
Methods inherited from View
Constructor Details
#initialize(total:, complete: "=", incomplete: " ", bar_format: :classic, label: nil) ⇒ Progressbar
Returns a new instance of Progressbar.
8 9 10 11 12 13 14 15 16 |
# File 'lib/charming/components/progressbar.rb', line 8 def initialize(total:, complete: "=", incomplete: " ", bar_format: :classic, label: nil) super() @total = [total.to_i, 0].max @complete = complete.to_s @incomplete = incomplete.to_s @bar_format = .to_sym @label = label @current = 0 end |
Instance Attribute Details
#bar_format ⇒ Object
Returns the value of attribute bar_format.
6 7 8 |
# File 'lib/charming/components/progressbar.rb', line 6 def @bar_format end |
#complete ⇒ Object
Returns the value of attribute complete.
6 7 8 |
# File 'lib/charming/components/progressbar.rb', line 6 def complete @complete end |
#current ⇒ Object
Returns the value of attribute current.
6 7 8 |
# File 'lib/charming/components/progressbar.rb', line 6 def current @current end |
#incomplete ⇒ Object
Returns the value of attribute incomplete.
6 7 8 |
# File 'lib/charming/components/progressbar.rb', line 6 def incomplete @incomplete end |
#label ⇒ Object
Returns the value of attribute label.
6 7 8 |
# File 'lib/charming/components/progressbar.rb', line 6 def label @label end |
#total ⇒ Object
Returns the value of attribute total.
6 7 8 |
# File 'lib/charming/components/progressbar.rb', line 6 def total @total end |
Instance Method Details
#complete! ⇒ Object
28 29 30 31 |
# File 'lib/charming/components/progressbar.rb', line 28 def complete! @current = @total self end |
#render ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/charming/components/progressbar.rb', line 33 def render width = [@total, 1].max completed = completed_width(width) incomplete = width - completed incomplete -= 1 if @current.zero? = (@complete * completed) + (@incomplete * incomplete) result = "[" + + "]" return result unless @label "#{result} #{@label}" end |
#tick(count = 1) ⇒ Object
18 19 20 21 |
# File 'lib/charming/components/progressbar.rb', line 18 def tick(count = 1) update(@current + count) self end |
#update(value) ⇒ Object
23 24 25 26 |
# File 'lib/charming/components/progressbar.rb', line 23 def update(value) @current = value.to_i.clamp(0, @total) self end |