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.



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

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.



12
13
14
# File 'lib/ruby-progressbar/progress.rb', line 12

def running_average
  @running_average
end

#smoothingObject

Returns the value of attribute smoothing.



12
13
14
# File 'lib/ruby-progressbar/progress.rb', line 12

def smoothing
  @smoothing
end

#starting_positionObject

Returns the value of attribute starting_position.



12
13
14
# File 'lib/ruby-progressbar/progress.rb', line 12

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



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

def absolute
  progress - starting_position
end

#decrementObject



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

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



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

def finish
  self.progress = total unless unknown?
end

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  @progress == @total
end

#incrementObject



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

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)


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

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

#percentage_completedObject



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

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



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

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



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

def reset
  start :at => starting_position
end

#start(options = {}) ⇒ Object



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

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

#unknown?Boolean

Returns:

  • (Boolean)


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

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