Sha256: 361170a667b5972c8f2b0d577f244d022cae58cd7d3a930b5769f30768c6b60a

Contents?: true

Size: 713 Bytes

Versions: 2

Compression:

Stored size: 713 Bytes

Contents

module Enumerable
  def with_progress(args = {}, &block)
    if block_given?
      progresser = progress(args)
      begin
        ret = each { |*o|
          res = yield(*o)
          progresser.tick
          res
        }
      rescue
        progresser.fail
        raise
      end
      progresser.complete
      ret
    else
      enum_for(:with_progress, args)
    end
  end

  private
  def progress(args)
    @progress ||= begin
      # Lazily read the size, for some enumerable this may be quite expensive and
      # using this method should come with a warning in the documentation.
      total = self.size unless args[:total]
      @progress = Progressrus.new(total: total, **args)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
progressrus-1.0.3 lib/progressrus/core_ext/enumerable.rb
progressrus-1.0.2 lib/progressrus/core_ext/enumerable.rb