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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Progress

Returns a new instance of Progress.



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

def initialize(options = {})
  self.total = options.fetch(:total, DEFAULT_TOTAL)

  start(:at => DEFAULT_BEGINNING_POSITION)
end

Instance Attribute Details

#progressObject

Returns the value of attribute progress.



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

def progress
  @progress
end

#starting_positionObject

Returns the value of attribute starting_position.



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

def starting_position
  @starting_position
end

#totalObject

Returns the value of attribute total.



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

def total
  @total
end

Instance Method Details

#absoluteObject



104
105
106
# File 'lib/ruby-progressbar/progress.rb', line 104

def absolute
  progress - starting_position
end

#decrementObject



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

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



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

def finish
  self.progress = total unless unknown?
end

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  @progress == @total
end

#incrementObject



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

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)


85
86
87
# File 'lib/ruby-progressbar/progress.rb', line 85

def none?
  progress.zero?
end

#percentage_completedObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby-progressbar/progress.rb', line 73

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



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

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



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

def reset
  start(:at => starting_position)
end

#start(options = {}) ⇒ Object



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

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

#total_with_unknown_indicatorObject



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

def total_with_unknown_indicator
  total || '??'
end

#unknown?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/ruby-progressbar/progress.rb', line 89

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