Class: Charming::Presentation::Components::Progressbar

Inherits:
Charming::Presentation::Component show all
Defined in:
lib/charming/presentation/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.



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

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.



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

def bar_format
  @bar_format
end

#completeObject

Returns the value of attribute complete.



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

def complete
  @complete
end

#currentObject

Returns the value of attribute current.



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

def current
  @current
end

#incompleteObject

Returns the value of attribute incomplete.



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

def incomplete
  @incomplete
end

#labelObject

Returns the value of attribute label.



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

def label
  @label
end

#totalObject

Returns the value of attribute total.



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

def total
  @total
end

Instance Method Details

#complete!Object



29
30
31
32
# File 'lib/charming/presentation/components/progressbar.rb', line 29

def complete!
  @current = @total
  self
end

#renderObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/charming/presentation/components/progressbar.rb', line 34

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



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

def tick(count = 1)
  update(@current + count)
  self
end

#update(value) ⇒ Object



24
25
26
27
# File 'lib/charming/presentation/components/progressbar.rb', line 24

def update(value)
  @current = value.to_i.clamp(0, @total)
  self
end