class Progress::WithProgress
Handling with_progress
Attributes
enum[R]
enumerable[R]
title[R]
Public Class Methods
new(*args, &block)
click to toggle source
If block given run each on instance otherwise return instance
Calls superclass method
# File lib/progress/with_progress.rb, line 13 def self.new(*args, &block) block ? super.each(&block) : super end
new(enum, title = nil, length = nil)
click to toggle source
initialize with object responding to each, title and optional length if block is provided, it is passed to each
# File lib/progress/with_progress.rb, line 19 def initialize(enum, title = nil, length = nil) @enum, @title, @length = enum, title, length end
Public Instance Methods
in_threads(*args, &block)
click to toggle source
befriend with in_threads
gem
Calls superclass method
# File lib/progress/with_progress.rb, line 29 def in_threads(*args, &block) @enum.in_threads(*args).with_progress(@title, @length, &block) rescue NoMethodError super end
method_missing(method, *args, &block)
click to toggle source
Calls superclass method
# File lib/progress/with_progress.rb, line 45 def method_missing(method, *args, &block) if enumerable_method?(method) run(method, *args, &block) else super end end
respond_to?(method, include_private = false)
click to toggle source
Calls superclass method
# File lib/progress/with_progress.rb, line 40 def respond_to?(method, include_private = false) enumerable_method?(method) || super end
respond_to_missing?(method, include_private = false)
click to toggle source
Calls superclass method
# File lib/progress/with_progress.rb, line 35 def respond_to_missing?(method, include_private = false) enumerable_method?(method) || super end
with_progress(title = nil, length = nil, &block)
click to toggle source
returns self but changes title
# File lib/progress/with_progress.rb, line 24 def with_progress(title = nil, length = nil, &block) self.class.new(@enum, title, length || @length, &block) end
Protected Instance Methods
enum_length(enum)
click to toggle source
# File lib/progress/with_progress.rb, line 140 def enum_length(enum) enum.respond_to?(:size) && enum.size || enum.respond_to?(:length) && enum.length || enum.count end
enumerable_method?(method)
click to toggle source
# File lib/progress/with_progress.rb, line 55 def enumerable_method?(method) method == :each || Enumerable.method_defined?(method) end
io?()
click to toggle source
# File lib/progress/with_progress.rb, line 125 def io? @enum.respond_to?(:pos) && (@enum.respond_to?(:size) || @enum.respond_to?(:stat)) end
io_pos?()
click to toggle source
# File lib/progress/with_progress.rb, line 130 def io_pos? @enum.pos; true rescue Errno::ESPIPE false rescue Errno::EPIPE raise unless defined?(JRUBY_VERSION) false end
run(method, *args, &block)
click to toggle source
# File lib/progress/with_progress.rb, line 59 def run(method, *args, &block) case when !block run_without_block(@enum, method, *args) when @length run_with_length(@enum, @length, method, *args, &block) when @enum.is_a?(String) run_for_string(method, *args, &block) when io? run_for_io(method, *args, &block) else run_with_length(@enum, enum_length(@enum), method, *args, &block) end end
run_for_io(method, *args, &block)
click to toggle source
# File lib/progress/with_progress.rb, line 80 def run_for_io(method, *args, &block) if io_pos? run_with_pos(@enum, method, *args, &block) else warn "Progress: can't get #{@enum.class} pos, collecting elements" with_substitute(@enum.to_a) do |lines| run_with_length(lines, lines.length, method, *args, &block) end end end
run_for_string(method, *args, &block)
click to toggle source
# File lib/progress/with_progress.rb, line 74 def run_for_string(method, *args, &block) with_substitute(StringIO.new(@enum)) do |io| run_with_pos(io, method, *args, &block) end end
run_with_length(enum, length, method, *args) { |*block_args| ... }
click to toggle source
# File lib/progress/with_progress.rb, line 99 def run_with_length(enum, length, method, *args) Progress.start(@title, length) do enum.send(method, *args) do |*block_args| Progress.step do yield(*block_args) end end end end
run_with_pos(io, method, *args) { |*block_args| ... }
click to toggle source
# File lib/progress/with_progress.rb, line 109 def run_with_pos(io, method, *args) size = io.respond_to?(:size) ? io.size : io.stat.size Progress.start(@title, size) do io.send(method, *args) do |*block_args| Progress.set(io.pos) do yield(*block_args) end end end end
run_without_block(enum, method, *args)
click to toggle source
# File lib/progress/with_progress.rb, line 91 def run_without_block(enum, method, *args) Progress.start(@title) do Progress.step do enum.send(method, *args) end end end
with_substitute(enum) { |enum| ... }
click to toggle source
# File lib/progress/with_progress.rb, line 120 def with_substitute(enum) result = yield enum result.eql?(enum) ? @enum : result end