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_SMOOTHING =
0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Progress

Returns a new instance of Progress.



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

def initialize(options = {})
  self.total     = options.fetch(:total, DEFAULT_TOTAL)
  self.smoothing = options[:smoothing] || DEFAULT_SMOOTHING

  start :at => DEFAULT_BEGINNING_POSITION
end

Instance Attribute Details

#progressObject

Returns the value of attribute progress.



9
10
11
# File 'lib/ruby-progressbar/progress.rb', line 9

def progress
  @progress
end

#running_averageObject

Returns the value of attribute running_average.



9
10
11
# File 'lib/ruby-progressbar/progress.rb', line 9

def running_average
  @running_average
end

#smoothingObject

Returns the value of attribute smoothing.



9
10
11
# File 'lib/ruby-progressbar/progress.rb', line 9

def smoothing
  @smoothing
end

#starting_positionObject

Returns the value of attribute starting_position.



9
10
11
# File 'lib/ruby-progressbar/progress.rb', line 9

def starting_position
  @starting_position
end

#totalObject

Returns the value of attribute total.



9
10
11
# File 'lib/ruby-progressbar/progress.rb', line 9

def total
  @total
end

Instance Method Details

#absoluteObject



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

def absolute
  progress - starting_position
end

#decrementObject



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

def decrement
  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.' if progress == 0

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

#finishObject



28
29
30
# File 'lib/ruby-progressbar/progress.rb', line 28

def finish
  self.progress = total
end

#finished?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/ruby-progressbar/progress.rb', line 32

def finished?
  @progress == @total
end

#incrementObject



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

def increment
  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.' if progress == total

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

#none?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/ruby-progressbar/progress.rb', line 91

def none?
  progress.zero?
end

#percentage_completedObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ruby-progressbar/progress.rb', line 79

def percentage_completed
  return 0   if total.nil?
  return 100 if total.zero?

  # 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



99
100
101
102
103
104
# File 'lib/ruby-progressbar/progress.rb', line 99

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

  format('%5.2f', (progress.to_f * 100.0 / total * 100.0).floor / 100.0)
end

#resetObject



52
53
54
# File 'lib/ruby-progressbar/progress.rb', line 52

def reset
  start :at => starting_position
end

#start(options = {}) ⇒ Object



22
23
24
25
26
# File 'lib/ruby-progressbar/progress.rb', line 22

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

#unknown?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/ruby-progressbar/progress.rb', line 95

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