Sha256: 9322574830b946b56c2c60b0da2e61bb469a6d9b012e0618c49c8b6ffac5faf5

Contents?: true

Size: 904 Bytes

Versions: 8

Compression:

Stored size: 904 Bytes

Contents

require 'delegate'

class Progress
  class WithProgress
    attr_reader :object, :title
    def initialize(object, title)
      @object = Progress::Enhancer.new(object)
      @title = title
    end

    def with_progress(title)
      self
    end

    def method_missing(method, *args, &block)
      Progress.start(title, object.length) do
        object.send(method, *args, &block)
      end
    end
  end

  class Enhancer < SimpleDelegator
    include Enumerable
    def each(*args, &block)
      __getobj__.each(*args) do |*yielded|
        Progress.step do
          block.call(*yielded)
        end
      end
    end

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
progress-1.2.1 lib/progress/with_progress.rb
progress-1.2.0 lib/progress/with_progress.rb
progress-1.1.3 lib/progress/with_progress.rb
progress-1.1.2.1 lib/progress/with_progress.rb
progress-1.1.2 lib/progress/with_progress.rb
progress-1.1.1 lib/progress/with_progress.rb
progress-1.1.0 lib/progress/with_progress.rb
progress-1.0.1 lib/progress/with_progress.rb