Sha256: d24e60eb78d5196cc143a934706e7ab8ecdb68aaf037b4a64c14599a7a574871

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

require 'progress'
require 'delegate'

class Progress
  class WithProgress < Delegator
    include Enumerable

    attr_reader :enumerable, :title

    # initialize with object responding to each, title and optional length
    # if block is provided, it is passed to each
    def initialize(enumerable, title, length = nil, &block)
      super(enumerable)
      @enumerable, @title, @length = enumerable, title, length
      each(&block) if block
    end

    # each object with progress
    def each
      enumerable = case
      when @length
        @enumerable
      when
            @enumerable.is_a?(String),
            @enumerable.is_a?(IO),
            Object.const_defined?(:StringIO) && @enumerable.is_a?(StringIO),
            Object.const_defined?(:TempFile) && @enumerable.is_a?(TempFile)
        warn "Progress: collecting elements for instance of class #{@enumerable.class}"
        @enumerable.each.to_a
      else
        @enumerable
      end

      length = case
      when @length
        @length
      when enumerable.respond_to?(:size)
        enumerable.size
      when enumerable.respond_to?(:length)
        enumerable.length
      else
        enumerable.count
      end

      Progress.start(@title, length) do
        enumerable.each do |object|
          Progress.step do
            yield object
          end
        end
        @enumerable
      end
    end

    # returns self but changes title
    def with_progress(title = nil, length = nil, &block)
      self.class.new(@enumerable, title, length || @length, &block)
    end

    # befriend with in_threads gem
    def in_threads(*args, &block)
      @enumerable.in_threads(*args).with_progress(@title, @length, &block)
    rescue
      super
    end

  protected

    def __getobj__
      @enumerable
    end

    def __setobj__(obj)
      @enumerable = obj
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
progress-3.0.0 lib/progress/with_progress.rb