Module: ProgressBar::Components::Progressable
- Included in:
- Bar, EstimatedTimer
- Defined in:
- lib/ruby-progressbar/components/progressable.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
- #decrement ⇒ Object
- #finish ⇒ Object
- #finished? ⇒ Boolean
- #increment ⇒ Object
- #initialize(options = {}) ⇒ Object
- #percentage_completed ⇒ Object
- #percentage_completed_with_precision ⇒ Object
- #reset ⇒ Object
- #start(options = {}) ⇒ Object
- #started? ⇒ Boolean
Instance Attribute Details
#progress ⇒ Object
Returns the value of attribute progress.
11 12 13 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 11 def progress @progress end |
#running_average ⇒ Object
Returns the value of attribute running_average.
13 14 15 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 13 def running_average @running_average end |
#smoothing ⇒ Object
Returns the value of attribute smoothing.
14 15 16 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 14 def smoothing @smoothing end |
#starting_position ⇒ Object
Returns the value of attribute starting_position.
12 13 14 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 12 def starting_position @starting_position end |
#total ⇒ Object
Returns the value of attribute total.
10 11 12 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 10 def total @total end |
Instance Method Details
#decrement ⇒ Object
44 45 46 47 48 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 44 def decrement STDOUT.puts "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
67 68 69 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 67 def finish self.progress = self.total end |
#finished? ⇒ Boolean
34 35 36 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 34 def finished? self.progress == self.total end |
#increment ⇒ Object
38 39 40 41 42 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 38 def increment STDOUT.puts "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 |
#initialize(options = {}) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 16 def initialize( = {}) self.total = .fetch(:total, DEFAULT_TOTAL) self.smoothing = [:smoothing] || DEFAULT_SMOOTHING start :at => DEFAULT_BEGINNING_POSITION end |
#percentage_completed ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 71 def percentage_completed return 100 if total == 0 return 0 if total.nil? # progress / total * 100 # # Doing this way so we can avoid converting each # number to a float and then back to an integer. # (self.progress * 100 / total).to_i end |
#percentage_completed_with_precision ⇒ Object
83 84 85 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 83 def percentage_completed_with_precision format('%5.2f', (progress.to_f * 100.0 / total * 100.0).floor / 100.0) end |
#reset ⇒ Object
50 51 52 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 50 def reset start :at => self.starting_position end |
#start(options = {}) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 23 def start( = {}) self.running_average = 0 self.progress = \ self.starting_position = [:at] || self.progress end |
#started? ⇒ Boolean
30 31 32 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 30 def started? !!self.starting_position end |