Class: ProgressBar
- Inherits:
-
Object
- Object
- ProgressBar
- Defined in:
- lib/progressbar.rb,
lib/web/progressbar4web.rb
Overview
コンソール用のプログレスバーはWEB UIでは使えないため置き換える
Defined Under Namespace
Classes: OverRangeError
Class Method Summary collapse
Instance Method Summary collapse
- #calc_ratio(num) ⇒ Object
- #clear ⇒ Object
-
#initialize(*args) ⇒ ProgressBar
constructor
A new instance of ProgressBar.
-
#original_initialize ⇒ ProgressBar
A new instance of ProgressBar.
- #output(num) ⇒ Object
- #silence? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ ProgressBar
Returns a new instance of ProgressBar.
9 10 11 12 13 14 15 |
# File 'lib/progressbar.rb', line 9 def initialize(max, interval = 1, width = 50, char = "*") @max = max == 0 ? 1.0 : max.to_f @interval = interval @width = width @char = char @counter = 0 end |
Class Method Details
.push_server=(server) ⇒ Object
12 13 14 |
# File 'lib/web/progressbar4web.rb', line 12 def self.push_server=(server) @@push_server = server end |
Instance Method Details
#calc_ratio(num) ⇒ Object
35 36 37 |
# File 'lib/progressbar.rb', line 35 def calc_ratio(num) num / @max end |
#clear ⇒ Object
30 31 32 33 |
# File 'lib/progressbar.rb', line 30 def clear return if silence? STDOUT.print " " * 79 + "\r" end |
#original_initialize ⇒ ProgressBar
Returns a new instance of ProgressBar.
16 17 18 19 20 21 22 |
# File 'lib/web/progressbar4web.rb', line 16 def initialize(max, interval = 1, width = 50, char = "*") @max = max == 0 ? 1.0 : max.to_f @interval = interval @width = width @char = char @counter = 0 end |
#output(num) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/progressbar.rb', line 17 def output(num) return if silence? if num > @max raise OverRangeError, "`#{num}` over `#{@max}(max)`" end @counter += 1 return unless @counter % @interval == 0 ratio = calc_ratio(num) now = (@width * ratio).round rest = @width - now STDOUT.print "[" + @char * now + ' ' * rest + "] #{(ratio * 100).round}%\r" end |
#silence? ⇒ Boolean
39 40 41 |
# File 'lib/progressbar.rb', line 39 def silence? $debug || ENV["NAROU_ENV"] == "test" end |