Sha256: e19074dd6a58db9cfa3c611007c41cbafbf99277e5837effa61964c702f42e2b

Contents?: true

Size: 819 Bytes

Versions: 1

Compression:

Stored size: 819 Bytes

Contents

require 'progress'

class Progress
  class WithProgress
    include Enumerable

    def initialize(enumerable, title, &block)
      @enumerable, @title = enumerable, title
      each(&block) if block
    end

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

    def length
      @length ||= if @enumerable.respond_to?(:length) && !@enumerable.is_a?(String)
        @enumerable.length
      elsif @enumerable.respond_to?(:count)
        @enumerable.count
      elsif @enumerable.respond_to?(:to_a)
        @enumerable.to_a.length
      else
        @enumerable.inject(0){ |length, obj| length + 1 }
      end
    end

    def with_progress(title = nil)
      self
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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