Class: ProgressBar::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-progressbar/progress.rb

Constant Summary collapse

DEFAULT_TOTAL =
100
DEFAULT_BEGINNING_POSITION =
0
DEFAULT_RUNNING_AVERAGE_RATE =
0.1
DEFAULT_RUNNING_AVERAGE_CALCULATOR =
ProgressBar::Calculators::SmoothedAverage
RUNNING_AVERAGE_CALCULATOR_MAP =
{
  'smoothing' => ProgressBar::Calculators::SmoothedAverage
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Progress

Returns a new instance of Progress.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby-progressbar/progress.rb', line 23

def initialize(options = {})
  self.total                      = options.fetch(:total, DEFAULT_TOTAL)
  self.running_average_rate       = options[:smoothing] ||
                                    options[:running_average_rate] ||
                                    DEFAULT_RUNNING_AVERAGE_RATE
  self.running_average_calculator = RUNNING_AVERAGE_CALCULATOR_MAP.
                                      fetch(options[:running_average_calculator],
                                            DEFAULT_RUNNING_AVERAGE_CALCULATOR)

  start :at => DEFAULT_BEGINNING_POSITION
end

Instance Attribute Details

#progressObject

Returns the value of attribute progress.



15
16
17
# File 'lib/ruby-progressbar/progress.rb', line 15

def progress
  @progress
end

#running_averageObject

Returns the value of attribute running_average.



18
19
20
# File 'lib/ruby-progressbar/progress.rb', line 18

def running_average
  @running_average
end

#running_average_calculatorObject

Returns the value of attribute running_average_calculator.



18
19
20
# File 'lib/ruby-progressbar/progress.rb', line 18

def running_average_calculator
  @running_average_calculator
end

#running_average_rateObject

Returns the value of attribute running_average_rate.



18
19
20
# File 'lib/ruby-progressbar/progress.rb', line 18

def running_average_rate
  @running_average_rate
end

#starting_positionObject

Returns the value of attribute starting_position.



18
19
20
# File 'lib/ruby-progressbar/progress.rb', line 18

def starting_position
  @starting_position
end

#totalObject

Returns the value of attribute total.



15
16
17
# File 'lib/ruby-progressbar/progress.rb', line 15

def total
  @total
end

Instance Method Details

#absoluteObject



126
127
128
# File 'lib/ruby-progressbar/progress.rb', line 126

def absolute
  progress - starting_position
end

#decrementObject



59
60
61
62
63
64
65
66
67
# File 'lib/ruby-progressbar/progress.rb', line 59

def decrement
  if progress == 0
    warn "WARNING: Your progress bar is currently at #{progress} out of #{total} " \
         "and cannot be decremented. In v2.0.0 this will become a " \
         "ProgressBar::InvalidProgressError."
  end

  self.progress -= 1 unless progress == 0
end

#finishObject



41
42
43
# File 'lib/ruby-progressbar/progress.rb', line 41

def finish
  self.progress = total unless unknown?
end

#finished?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ruby-progressbar/progress.rb', line 45

def finished?
  @progress == @total
end

#incrementObject



49
50
51
52
53
54
55
56
57
# File 'lib/ruby-progressbar/progress.rb', line 49

def increment
  if progress == total
    warn "WARNING: Your progress bar is currently at #{progress} out of #{total} " \
         "and cannot be incremented. In v2.0.0 this will become a " \
         "ProgressBar::InvalidProgressError."
  end

  self.progress += 1 unless progress == total
end

#none?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/ruby-progressbar/progress.rb', line 107

def none?
  running_average.zero? || progress.zero?
end

#percentage_completedObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby-progressbar/progress.rb', line 95

def percentage_completed
  return 0   if total.nil?
  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.
  #
  (progress * 100 / total).to_i
end

#percentage_completed_with_precisionObject



119
120
121
122
123
124
# File 'lib/ruby-progressbar/progress.rb', line 119

def percentage_completed_with_precision
  return 100.0  if total == 0
  return 0.0    if total.nil?

  '%5.2f' % [(progress * 100 / total.to_f * 100).floor / 100.0]
end

#resetObject



69
70
71
# File 'lib/ruby-progressbar/progress.rb', line 69

def reset
  start :at => starting_position
end

#start(options = {}) ⇒ Object



35
36
37
38
39
# File 'lib/ruby-progressbar/progress.rb', line 35

def start(options = {})
  self.running_average   = 0
  self.progress          = \
    self.starting_position = options[:at] || progress
end

#total_with_unknown_indicatorObject



115
116
117
# File 'lib/ruby-progressbar/progress.rb', line 115

def total_with_unknown_indicator
  total || '??'
end

#unknown?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/ruby-progressbar/progress.rb', line 111

def unknown?
  progress.nil? || total.nil?
end