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
Instance Attribute Summary collapse
-
#progress ⇒ Object
Returns the value of attribute progress.
-
#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.
12 13 14 15 16 |
# File 'lib/ruby-progressbar/progress.rb', line 12 def initialize( = {}) self.total = .fetch(:total, DEFAULT_TOTAL) start(:at => DEFAULT_BEGINNING_POSITION) end |
Instance Attribute Details
#progress ⇒ Object
Returns the value of attribute progress.
8 9 10 |
# File 'lib/ruby-progressbar/progress.rb', line 8 def progress @progress end |
#starting_position ⇒ Object
Returns the value of attribute starting_position.
10 11 12 |
# File 'lib/ruby-progressbar/progress.rb', line 10 def starting_position @starting_position end |
#total ⇒ Object
Returns the value of attribute total.
8 9 10 |
# File 'lib/ruby-progressbar/progress.rb', line 8 def total @total end |
Instance Method Details
#absolute ⇒ Object
104 105 106 |
# File 'lib/ruby-progressbar/progress.rb', line 104 def absolute progress - starting_position end |
#decrement ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/ruby-progressbar/progress.rb', line 41 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
23 24 25 |
# File 'lib/ruby-progressbar/progress.rb', line 23 def finish self.progress = total unless unknown? end |
#finished? ⇒ Boolean
27 28 29 |
# File 'lib/ruby-progressbar/progress.rb', line 27 def finished? @progress == @total end |
#increment ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/ruby-progressbar/progress.rb', line 31 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
85 86 87 |
# File 'lib/ruby-progressbar/progress.rb', line 85 def none? progress.zero? end |
#percentage_completed ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ruby-progressbar/progress.rb', line 73 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
97 98 99 100 101 102 |
# File 'lib/ruby-progressbar/progress.rb', line 97 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
51 52 53 |
# File 'lib/ruby-progressbar/progress.rb', line 51 def reset start(:at => starting_position) end |
#start(options = {}) ⇒ Object
18 19 20 21 |
# File 'lib/ruby-progressbar/progress.rb', line 18 def start( = {}) self.progress = \ self.starting_position = [:at] || progress end |
#total_with_unknown_indicator ⇒ Object
93 94 95 |
# File 'lib/ruby-progressbar/progress.rb', line 93 def total_with_unknown_indicator total || '??' end |
#unknown? ⇒ Boolean
89 90 91 |
# File 'lib/ruby-progressbar/progress.rb', line 89 def unknown? progress.nil? || total.nil? end |