Class: Averager
- Inherits:
-
Object
- Object
- Averager
- Defined in:
- lib/averager.rb
Defined Under Namespace
Modules: ObjectExtensions
Constant Summary collapse
- DEFAULT_EVERY =
1.0
- DEFAULT_DIGITS =
7
Instance Attribute Summary collapse
-
#counter ⇒ Object
Returns the value of attribute counter.
-
#digits ⇒ Object
Returns the value of attribute digits.
-
#every ⇒ Object
Returns the value of attribute every.
-
#expected ⇒ Object
Returns the value of attribute expected.
-
#log_path ⇒ Object
Returns the value of attribute log_path.
-
#progress_bar ⇒ Object
Returns the value of attribute progress_bar.
-
#stream ⇒ Object
Returns the value of attribute stream.
Instance Method Summary collapse
- #avg(*args) ⇒ Object
- #finish ⇒ Object
- #flush_stream_for_progress ⇒ Object
-
#initialize(options = {}) ⇒ Averager
constructor
A new instance of Averager.
- #print_current(status = nil) ⇒ Object
- #print_current? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Averager
Returns a new instance of Averager.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/averager.rb', line 22 def initialize( = {}) @started = Time.now self.every = [:every] || DEFAULT_EVERY self.expected = [:expected] self.digits = [:digits] if [:digits] self.digits ||= DEFAULT_DIGITS self.stream = [:stream] || $stdout self.log_path = [:log_path] self. = [:progress_bar] == true self.counter = 0 flush_stream_for_progress if block_given? yield(self) self.finish end end |
Instance Attribute Details
#counter ⇒ Object
Returns the value of attribute counter.
5 6 7 |
# File 'lib/averager.rb', line 5 def counter @counter end |
#digits ⇒ Object
Returns the value of attribute digits.
5 6 7 |
# File 'lib/averager.rb', line 5 def digits @digits end |
#every ⇒ Object
Returns the value of attribute every.
5 6 7 |
# File 'lib/averager.rb', line 5 def every @every end |
#expected ⇒ Object
Returns the value of attribute expected.
5 6 7 |
# File 'lib/averager.rb', line 5 def expected @expected end |
#log_path ⇒ Object
Returns the value of attribute log_path.
5 6 7 |
# File 'lib/averager.rb', line 5 def log_path @log_path end |
#progress_bar ⇒ Object
Returns the value of attribute progress_bar.
5 6 7 |
# File 'lib/averager.rb', line 5 def @progress_bar end |
#stream ⇒ Object
Returns the value of attribute stream.
5 6 7 |
# File 'lib/averager.rb', line 5 def stream @stream end |
Instance Method Details
#avg(*args) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/averager.rb', line 100 def avg(*args) status = nil i_or_status = args.shift if args.any? status = args.shift end if i_or_status.is_a?(Numeric) self.counter = i_or_status else if i_or_status.is_a?(String) status = i_or_status end self.counter += 1 end if print_current? print_current(status) @printed_last = true else @printed_last = false end @printed_last end |
#finish ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/averager.rb', line 123 def finish if !@printed_last && !print_current? print_current end @stream.puts "\n" if self. @stream.puts "finished in #{Time.now - @started}" @stream.close if ![$stdout, $stderr].include?(@stream) end |
#flush_stream_for_progress ⇒ Object
53 54 55 |
# File 'lib/averager.rb', line 53 def flush_stream_for_progress self.stream.print("\r") if self.log_path.nil? end |
#print_current(status = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/averager.rb', line 66 def print_current(status = nil) return if self.counter == 0 per_second = self.counter / (Time.now - @started) out = nil if block_given? out = yield(:digits => self.digits, :iteration => self.counter, :per_second => per_second, :status => status) else out = "%#{self.digits}d" % self.counter if @expected out << "/#{@expected}" out << " %3.1f%" % (100 * (self.counter / @expected.to_f)) if self.counter <= @expected end out << " (%.1f/second" % [per_second] if !per_second.infinite? && @expected && self.counter < @expected missing = @expected - self.counter seconds = missing / per_second hours = (seconds / 3600.0).floor seconds -= (hours * 3600) minutes = (seconds / 60.0).floor seconds -= (minutes * 60) out << " %02d:%02d:%02d" % [hours, minutes, seconds] end out << ")" out << ": #{status}" if status end if self. flush_stream_for_progress @stream.print out else @stream.puts out end @stream.flush end |
#print_current? ⇒ Boolean
57 58 59 60 61 62 63 64 |
# File 'lib/averager.rb', line 57 def print_current? if @last_printed.nil? || (Time.now - @last_printed) >= self.every @last_printed = Time.now true else false end end |