class Popro::Info

Attributes

current[RW]
total[RW]

Public Class Methods

new(total: nil, current: nil) click to toggle source
# File lib/popro/info.rb, line 7
def initialize(total: nil, current: nil)
  @total = total
  @current = current || 0
  @started = false
end

Public Instance Methods

finish() click to toggle source
# File lib/popro/info.rb, line 22
def finish
  @started = false
  self
end
next(num = 1) click to toggle source
# File lib/popro/info.rb, line 51
def next(num = 1)
  raise TypeError, 'expected an integer' unless num.is_a? Integer

  @current += num
  self
end
pct() click to toggle source
# File lib/popro/info.rb, line 27
def pct
  return 0 if @total.nil? || @total.zero?

  @current.to_f / @total
end
pct_formatted() click to toggle source
# File lib/popro/info.rb, line 33
def pct_formatted
  percentage = pct
  return nil if percentage.nil?

  format('%<percent>.1f', percent: pct * 100)
end
running?() click to toggle source
# File lib/popro/info.rb, line 13
def running?
  @started
end
start() click to toggle source
# File lib/popro/info.rb, line 17
def start
  @started = true
  self
end
to_f() click to toggle source
# File lib/popro/info.rb, line 47
def to_f
  pct
end
to_h() click to toggle source
# File lib/popro/info.rb, line 58
def to_h
  {
    pct: pct,
    pct_formatted: pct_formatted,
    current: @current,
    total: @total
  }
end
total_length() click to toggle source
# File lib/popro/info.rb, line 40
def total_length
  num = [@total, @current].max
  return 1 if num.zero?

  Math.log10(num + 1).ceil
end