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_RUNNING_AVERAGE_RATE =
0.1
- DEFAULT_RUNNING_AVERAGE_CALCULATOR =
ProgressBar::Calculators::SmoothedAverage
- RUNNING_AVERAGE_CALCULATOR_MAP =
{ 'smoothing' => ProgressBar::Calculators::SmoothedAverage }.freeze
Instance Attribute Summary collapse
-
#progress ⇒ Object
Returns the value of attribute progress.
-
#running_average ⇒ Object
Returns the value of attribute running_average.
-
#running_average_calculator ⇒ Object
Returns the value of attribute running_average_calculator.
-
#running_average_rate ⇒ Object
Returns the value of attribute running_average_rate.
-
#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
- #total_with_unknown_indicator ⇒ Object
- #unknown? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Progress
Returns a new instance of Progress.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ruby-progressbar/progress.rb', line 23 def initialize( = {}) self.total = .fetch(:total, DEFAULT_TOTAL) self.running_average_rate = [:smoothing] || [:running_average_rate] || DEFAULT_RUNNING_AVERAGE_RATE self.running_average_calculator = RUNNING_AVERAGE_CALCULATOR_MAP. fetch([:running_average_calculator], DEFAULT_RUNNING_AVERAGE_CALCULATOR) start :at => DEFAULT_BEGINNING_POSITION end |
Instance Attribute Details
#progress ⇒ Object
Returns the value of attribute progress.
15 16 17 |
# File 'lib/ruby-progressbar/progress.rb', line 15 def progress @progress end |
#running_average ⇒ Object
Returns the value of attribute running_average.
18 19 20 |
# File 'lib/ruby-progressbar/progress.rb', line 18 def running_average @running_average end |
#running_average_calculator ⇒ Object
Returns the value of attribute running_average_calculator.
18 19 20 |
# File 'lib/ruby-progressbar/progress.rb', line 18 def running_average_calculator @running_average_calculator end |
#running_average_rate ⇒ Object
Returns the value of attribute running_average_rate.
18 19 20 |
# File 'lib/ruby-progressbar/progress.rb', line 18 def running_average_rate @running_average_rate end |
#starting_position ⇒ Object
Returns the value of attribute starting_position.
18 19 20 |
# File 'lib/ruby-progressbar/progress.rb', line 18 def starting_position @starting_position end |
#total ⇒ Object
Returns the value of attribute total.
15 16 17 |
# File 'lib/ruby-progressbar/progress.rb', line 15 def total @total end |
Instance Method Details
#absolute ⇒ Object
126 127 128 |
# File 'lib/ruby-progressbar/progress.rb', line 126 def absolute progress - starting_position end |
#decrement ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby-progressbar/progress.rb', line 59 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
41 42 43 |
# File 'lib/ruby-progressbar/progress.rb', line 41 def finish self.progress = total unless unknown? end |
#finished? ⇒ Boolean
45 46 47 |
# File 'lib/ruby-progressbar/progress.rb', line 45 def finished? @progress == @total end |
#increment ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/ruby-progressbar/progress.rb', line 49 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
107 108 109 |
# File 'lib/ruby-progressbar/progress.rb', line 107 def none? running_average.zero? || progress.zero? end |
#percentage_completed ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ruby-progressbar/progress.rb', line 95 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
119 120 121 122 123 124 |
# File 'lib/ruby-progressbar/progress.rb', line 119 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
69 70 71 |
# File 'lib/ruby-progressbar/progress.rb', line 69 def reset start :at => starting_position end |
#start(options = {}) ⇒ Object
35 36 37 38 39 |
# File 'lib/ruby-progressbar/progress.rb', line 35 def start( = {}) self.running_average = 0 self.progress = \ self.starting_position = [:at] || progress end |
#total_with_unknown_indicator ⇒ Object
115 116 117 |
# File 'lib/ruby-progressbar/progress.rb', line 115 def total_with_unknown_indicator total || '??' end |
#unknown? ⇒ Boolean
111 112 113 |
# File 'lib/ruby-progressbar/progress.rb', line 111 def unknown? progress.nil? || total.nil? end |