Sha256: 1f6084e5a1d2600ab7825727c1f1ee92290fc02dc82a4ee0bd607bbfe13ec4c4

Contents?: true

Size: 887 Bytes

Versions: 19

Compression:

Stored size: 887 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|
        block.call(*yielded)
        Progress.step
      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

19 entries across 19 versions & 1 rubygems

Version Path
progress-1.0.0 lib/progress/with_progress.rb
progress-0.4.1 lib/progress/with_progress.rb
progress-0.3.0 lib/progress/with_progress.rb
progress-0.2.2 lib/progress/with_progress.rb
progress-0.2.1 lib/progress/with_progress.rb
progress-0.2.0 lib/progress/with_progress.rb
progress-0.1.2 lib/progress/with_progress.rb
progress-0.1.1.3 lib/progress/with_progress.rb
progress-0.1.1.2 lib/progress/with_progress.rb
progress-0.1.1.1 lib/progress/with_progress.rb
progress-0.1.1.0 lib/progress/with_progress.rb
progress-0.1.0.3 lib/progress/with_progress.rb
progress-0.1.0.0 lib/progress/with_progress.rb
progress-0.1.0.1 lib/progress/with_progress.rb
progress-0.1.0.2 lib/progress/with_progress.rb
progress-0.0.9.3 lib/progress/with_progress.rb
progress-0.0.9.0 lib/progress/with_progress.rb
progress-0.0.9.1 lib/progress/with_progress.rb
progress-0.0.9.2 lib/progress/with_progress.rb