class Fortschritt::Meter

Attributes

average_seconds[RW]
done[RW]
silent[RW]
total[RW]
updated_at[RW]

Public Class Methods

new(total, silent: false) click to toggle source
# File lib/fortschritt/meter.rb, line 5
def initialize(total, silent: false)
  @total           = total
  @done            = 0
  @updated_at      = Time.now
  @average_seconds = 0
  @started_at      = Time.now
  @silent          = silent || !!(Rails.env.test? if defined?(Rails))
end

Public Instance Methods

calculate_average_seconds(value) click to toggle source
# File lib/fortschritt/meter.rb, line 35
def calculate_average_seconds(value)
  average_seconds.zero? and return value
  ((average_seconds * done) + value) / (done + 1)
end
completed?() click to toggle source
# File lib/fortschritt/meter.rb, line 23
def completed?
  done >= total
end
increment() click to toggle source
# File lib/fortschritt/meter.rb, line 14
def increment
  @_now            = Time.now
  elapsed_seconds  = @_now - updated_at
  @average_seconds = calculate_average_seconds(elapsed_seconds)
  @updated_at      = @_now
  @done           += 1
  print! unless @silent
end
print!() click to toggle source
remaining() click to toggle source
# File lib/fortschritt/meter.rb, line 31
def remaining
  [total - done, 0].max
end
remaining_seconds() click to toggle source
# File lib/fortschritt/meter.rb, line 27
def remaining_seconds
  remaining * average_seconds
end
total_elapsed_seconds() click to toggle source
# File lib/fortschritt/meter.rb, line 40
def total_elapsed_seconds
  Time.now - @started_at
end