class Popro::Context

Public Class Methods

new(progress:, info:, indicator:, step: 1) click to toggle source
# File lib/popro/context.rb, line 11
def initialize(progress:, info:, indicator:, step: 1)
  @progress = progress
  @info = info
  @step = step
  @_indicator = indicator
end

Public Instance Methods

did(yielded = nil, amount = nil) click to toggle source
# File lib/popro/context.rb, line 56
def did(yielded = nil, amount = nil)
  @info.start unless @info.running?
  amount = @step if amount.nil?
  raise TypeError, "amount: expected an integer, got #{amount.class}" unless amount.is_a? Integer

  @info.current += amount unless amount.zero?
  indicator.call(@info, yielded)

  self
end
done() click to toggle source
# File lib/popro/context.rb, line 40
def done
  raise OutOfSyncError, 'done while not started' unless @info.running?

  @info.finish
  indicator.finish if indicator.respond_to? :finish
end
each(obj, total = nil, &block) click to toggle source
# File lib/popro/context.rb, line 18
def each(obj, total = nil, &block)
  _each(obj, total) do |*args|
    did block.call(*args, progress: @info)
  end
end
each_gonna(obj, titler, total = nil, &block) click to toggle source
# File lib/popro/context.rb, line 73
def each_gonna(obj, titler, total = nil, &block)
  _each(obj, total) do |*args|
    gonna(titler.call(*args))
    block.call(*args, progress: @info)
  end
end
each_will(obj, titler, total = nil, &block) click to toggle source
# File lib/popro/context.rb, line 24
def each_will(obj, titler, total = nil, &block)
  _each(obj, total) do |*args|
    title = titler.call(*args)
    will(title) do
      block.call(*args, progress: @info)
      nil
    end
  end
end
formatter(&block) click to toggle source
# File lib/popro/context.rb, line 47
def formatter(&block)
  unless @_indicator.respond_to?(:formatter=)
    raise ConfigError, "seems formatter is not available for #{@_indicator.class}"
  end

  @_indicator.formatter = block
  block
end
gonna(title) click to toggle source
# File lib/popro/context.rb, line 67
def gonna(title)
  @info.start unless @info.running?
  indicator.call(@info, title)
  self
end
start() click to toggle source
# File lib/popro/context.rb, line 34
def start
  raise OutOfSyncError, 'trying to start when already running' if @info.running?

  @info.start
end
to_proc() click to toggle source
# File lib/popro/context.rb, line 91
def to_proc
  proc { |yielded| did(yielded) }
end
will(title = nil, step = nil, &block) click to toggle source
# File lib/popro/context.rb, line 80
def will(title = nil, step = nil, &block)
  gonna "#{WILL_CHECK_MARKS[0]} #{title}"

  return self unless block_given?

  block.call
  yielded = "#{WILL_CHECK_MARKS[1]} #{title}"
  did(yielded, step || @step)
  yielded
end

Private Instance Methods

_each(obj, total = nil, &block) click to toggle source
# File lib/popro/context.rb, line 103
def _each(obj, total = nil, &block)
  total = obj.size if total.nil?
  raise TypeError, "total: expected an integer got #{total.class}" unless total.is_a?(Integer) || total.nil?

  @info.total += total if total.positive?
  # block = proc { |d| d } unless block_given?

  obj.each do |*args, **kwargs|
    block.call(*args, **kwargs)
  end

  self
end
indicator() click to toggle source
# File lib/popro/context.rb, line 97
def indicator
  return Indicator::Null if Popro.silenced?

  @_indicator
end