module ProgressBar::Components::Progressable

Constants

DEFAULT_BEGINNING_POSITION
DEFAULT_SMOOTHING
DEFAULT_TOTAL

Attributes

progress[R]
running_average[RW]
smoothing[RW]
starting_position[RW]
total[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/progress_bar/components/progressable.rb, line 14
def initialize(options = {})
  self.total           = options[:total]     || DEFAULT_TOTAL
  self.smoothing       = options[:smoothing] || DEFAULT_SMOOTHING

  start :at => DEFAULT_BEGINNING_POSITION
end

Public Instance Methods

decrement() click to toggle source
# File lib/progress_bar/components/progressable.rb, line 36
def decrement
  self.progress -= 1 unless progress == 0
end
finish() click to toggle source
# File lib/progress_bar/components/progressable.rb, line 57
def finish
  self.progress = self.total
end
increment() click to toggle source
# File lib/progress_bar/components/progressable.rb, line 32
def increment
  self.progress += 1 unless progress == total
end
percentage_completed() click to toggle source
# File lib/progress_bar/components/progressable.rb, line 61
def percentage_completed
  return 100 if total == 0

  # progress / total * 100
  #
  # Doing this way so we can avoid converting each
  # number to a float and then back to an integer.
  #
  self.progress * 100 / total
end
percentage_completed_with_precision() click to toggle source
# File lib/progress_bar/components/progressable.rb, line 72
def percentage_completed_with_precision
  format('%5.2f', (progress.to_f * 100.0 / total * 100.0).floor / 100.0)
end
progress=(new_progress) click to toggle source
# File lib/progress_bar/components/progressable.rb, line 44
def progress=(new_progress)
  validate_progress(new_progress)

  @progress = new_progress

  update_running_average
end
reset() click to toggle source
# File lib/progress_bar/components/progressable.rb, line 40
def reset
  start :at => self.starting_position
end
start(options = {}) click to toggle source
# File lib/progress_bar/components/progressable.rb, line 21
def start(options = {})
  self.running_average   = 0

  self.progress          =          self.starting_position = options[:at] || self.progress
end
started?() click to toggle source
# File lib/progress_bar/components/progressable.rb, line 28
def started?
  !!self.starting_position
end
total=(new_total) click to toggle source
# File lib/progress_bar/components/progressable.rb, line 52
def total=(new_total)
  validate_total(new_total)
  @total = new_total
end