Class: ProgressBar::Progress
- Inherits:
-
Object
- Object
- ProgressBar::Progress
- 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
-
#progress ⇒ Object
Returns the value of attribute progress.
-
#running_average ⇒ Object
Returns the value of attribute running_average.
-
#smoothing ⇒ Object
Returns the value of attribute smoothing.
-
#starting_position ⇒ Object
Returns the value of attribute starting_position.
-
#total ⇒ Object
Returns the value of attribute total.
Instance Method Summary collapse
- #absolute ⇒ Object
- #decrement ⇒ Object
- #finish ⇒ Object
- #finished? ⇒ Boolean
- #increment ⇒ Object
-
#initialize(options = {}) ⇒ Progress
constructor
A new instance of Progress.
- #none? ⇒ Boolean
- #percentage_completed ⇒ Object
- #percentage_completed_with_precision ⇒ Object
- #reset ⇒ Object
- #start(options = {}) ⇒ Object
- #unknown? ⇒ Boolean
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( = {}) self.total = .fetch(:total, DEFAULT_TOTAL) self.smoothing = [:smoothing] || DEFAULT_SMOOTHING start :at => DEFAULT_BEGINNING_POSITION end |
Instance Attribute Details
#progress ⇒ Object
Returns the value of attribute progress.
9 10 11 |
# File 'lib/ruby-progressbar/progress.rb', line 9 def progress @progress end |
#running_average ⇒ Object
Returns the value of attribute running_average.
12 13 14 |
# File 'lib/ruby-progressbar/progress.rb', line 12 def running_average @running_average end |
#smoothing ⇒ Object
Returns the value of attribute smoothing.
12 13 14 |
# File 'lib/ruby-progressbar/progress.rb', line 12 def smoothing @smoothing end |
#starting_position ⇒ Object
Returns the value of attribute starting_position.
12 13 14 |
# File 'lib/ruby-progressbar/progress.rb', line 12 def starting_position @starting_position end |
#total ⇒ Object
Returns the value of attribute total.
9 10 11 |
# File 'lib/ruby-progressbar/progress.rb', line 9 def total @total end |
Instance Method Details
#absolute ⇒ Object
110 111 112 |
# File 'lib/ruby-progressbar/progress.rb', line 110 def absolute progress - starting_position end |
#decrement ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/ruby-progressbar/progress.rb', line 47 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 |
#finish ⇒ Object
29 30 31 |
# File 'lib/ruby-progressbar/progress.rb', line 29 def finish self.progress = total unless unknown? end |
#finished? ⇒ Boolean
33 34 35 |
# File 'lib/ruby-progressbar/progress.rb', line 33 def finished? @progress == @total end |
#increment ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/ruby-progressbar/progress.rb', line 37 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
95 96 97 |
# File 'lib/ruby-progressbar/progress.rb', line 95 def none? running_average.zero? || progress.zero? end |
#percentage_completed ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ruby-progressbar/progress.rb', line 83 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_precision ⇒ Object
103 104 105 106 107 108 |
# File 'lib/ruby-progressbar/progress.rb', line 103 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 |
#reset ⇒ Object
57 58 59 |
# File 'lib/ruby-progressbar/progress.rb', line 57 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( = {}) self.running_average = 0 self.progress = \ self.starting_position = [:at] || progress end |
#unknown? ⇒ Boolean
99 100 101 |
# File 'lib/ruby-progressbar/progress.rb', line 99 def unknown? progress.nil? || total.nil? end |