Module: ProgressBar::Components::Progressable
- Included in:
- Bar, EstimatedTimer, Rate
- 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
-
#autofinish ⇒ Object
Returns the value of attribute autofinish.
-
#finished ⇒ Object
Returns the value of attribute finished.
-
#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
- #progress_made ⇒ Object
- #reset ⇒ Object
- #start(options = {}) ⇒ Object
- #started? ⇒ Boolean
Instance Attribute Details
#autofinish ⇒ Object
Returns the value of attribute autofinish.
15 16 17 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 15 def autofinish @autofinish end |
#finished ⇒ Object
Returns the value of attribute finished.
16 17 18 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 16 def finished @finished end |
#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
47 48 49 50 51 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 47 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
70 71 72 73 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 70 def finish self.finished = true self.progress = total end |
#finished? ⇒ Boolean
37 38 39 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 37 def finished? finished || (autofinish && progress == total) end |
#increment ⇒ Object
41 42 43 44 45 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 41 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 |
#initialize(options = {}) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 18 def initialize( = {}) self.total = .fetch(:total, DEFAULT_TOTAL) self.smoothing = [:smoothing] || DEFAULT_SMOOTHING self.autofinish = .fetch(:autofinish, true) start :at => DEFAULT_BEGINNING_POSITION end |
#percentage_completed ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 75 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
87 88 89 90 91 92 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 87 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 |
#progress_made ⇒ Object
94 95 96 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 94 def progress_made started? ? self.progress - self.starting_position : 0 end |
#reset ⇒ Object
53 54 55 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 53 def reset start :at => self.starting_position end |
#start(options = {}) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 26 def start( = {}) self.finished = false self.running_average = 0 self.progress = \ self.starting_position = [:at] || self.progress end |
#started? ⇒ Boolean
33 34 35 |
# File 'lib/ruby-progressbar/components/progressable.rb', line 33 def started? !!self.starting_position end |