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.
15 16 17 18 19 20 |
# File 'lib/ruby-progressbar/progress.rb', line 15 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.
9 10 11 |
# File 'lib/ruby-progressbar/progress.rb', line 9 def running_average @running_average end |
#smoothing ⇒ Object
Returns the value of attribute smoothing.
9 10 11 |
# File 'lib/ruby-progressbar/progress.rb', line 9 def smoothing @smoothing end |
#starting_position ⇒ Object
Returns the value of attribute starting_position.
9 10 11 |
# File 'lib/ruby-progressbar/progress.rb', line 9 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
106 107 108 |
# File 'lib/ruby-progressbar/progress.rb', line 106 def absolute progress - starting_position end |
#decrement ⇒ Object
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 |
#finish ⇒ Object
28 29 30 |
# File 'lib/ruby-progressbar/progress.rb', line 28 def finish self.progress = total end |
#finished? ⇒ Boolean
32 33 34 |
# File 'lib/ruby-progressbar/progress.rb', line 32 def finished? @progress == @total end |
#increment ⇒ Object
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
91 92 93 |
# File 'lib/ruby-progressbar/progress.rb', line 91 def none? progress.zero? end |
#percentage_completed ⇒ Object
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_precision ⇒ Object
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 |
#reset ⇒ Object
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( = {}) self.running_average = 0 self.progress = \ self.starting_position = [:at] || progress end |
#unknown? ⇒ Boolean
95 96 97 |
# File 'lib/ruby-progressbar/progress.rb', line 95 def unknown? progress.nil? || total.nil? end |