Class: Averager

Inherits:
Object
  • Object
show all
Defined in:
lib/averager.rb

Defined Under Namespace

Modules: ObjectExtensions

Constant Summary collapse

DEFAULT_EVERY =
1.0
DEFAULT_DIGITS =
7

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @started = Time.now
  self.every = options[:every] || DEFAULT_EVERY
  self.expected = options[:expected]
  self.digits = options[:digits] if options[:digits]
  self.digits ||= DEFAULT_DIGITS
  self.stream = options[:stream] || $stdout
  self.log_path = options[:log_path]
  self.progress_bar = options[:progress_bar] == true
  self.counter = 0
  flush_stream_for_progress
  if block_given?
    yield(self)
    self.finish
  end
end

Instance Attribute Details

#counterObject

Returns the value of attribute counter.



5
6
7
# File 'lib/averager.rb', line 5

def counter
  @counter
end

#digitsObject

Returns the value of attribute digits.



5
6
7
# File 'lib/averager.rb', line 5

def digits
  @digits
end

#everyObject

Returns the value of attribute every.



5
6
7
# File 'lib/averager.rb', line 5

def every
  @every
end

#expectedObject

Returns the value of attribute expected.



5
6
7
# File 'lib/averager.rb', line 5

def expected
  @expected
end

#log_pathObject

Returns the value of attribute log_path.



5
6
7
# File 'lib/averager.rb', line 5

def log_path
  @log_path
end

#progress_barObject

Returns the value of attribute progress_bar.



5
6
7
# File 'lib/averager.rb', line 5

def progress_bar
  @progress_bar
end

#streamObject

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

#finishObject



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.progress_bar
  @stream.puts "finished in #{Time.now - @started}"
  @stream.close if ![$stdout, $stderr].include?(@stream)
end

#flush_stream_for_progressObject



53
54
55
# File 'lib/averager.rb', line 53

def flush_stream_for_progress
  self.stream.print("\r") if self.log_path.nil?
end


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.progress_bar
    flush_stream_for_progress
    @stream.print out
  else
    @stream.puts out
  end
  @stream.flush
end

Returns:

  • (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