Class: Charming::Components::Progressbar

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from View

#focused?, #layout_assigns

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 = bar_format.to_sym
  @label = label
  @current = 0
end

Instance Attribute Details

#bar_formatObject

Returns the value of attribute bar_format.



6
7
8
# File 'lib/charming/components/progressbar.rb', line 6

def bar_format
  @bar_format
end

#completeObject

Returns the value of attribute complete.



6
7
8
# File 'lib/charming/components/progressbar.rb', line 6

def complete
  @complete
end

#currentObject

Returns the value of attribute current.



6
7
8
# File 'lib/charming/components/progressbar.rb', line 6

def current
  @current
end

#incompleteObject

Returns the value of attribute incomplete.



6
7
8
# File 'lib/charming/components/progressbar.rb', line 6

def incomplete
  @incomplete
end

#labelObject

Returns the value of attribute label.



6
7
8
# File 'lib/charming/components/progressbar.rb', line 6

def label
  @label
end

#totalObject

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

#renderObject



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?
  bar = (@complete * completed) + (@incomplete * incomplete)
  result = "[" + bar + "]"

  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