lib/tracksperanto/progressive_io.rb in tracksperanto-1.8.1 vs lib/tracksperanto/progressive_io.rb in tracksperanto-1.8.2
- old
+ new
@@ -1,19 +1,19 @@
# Used for IO objects that need to report the current offset at each operation that changes the said offset
# (useful for building progress bars that report on a file read operation)
class Tracksperanto::ProgressiveIO < DelegateClass(IO)
+ include Tracksperanto::Returning
- # Should contain a block that accepts the current offset in bytes and the total size
- attr_accessor :progress_block
-
# Get or set the total size of the contained IO. If the passed IO is a File object
# the size will be preset automatically
attr_accessor :total_size
+ attr_accessor :progress_block
- def initialize(with_file)
+ def initialize(with_file, &blk)
__setobj__(with_file)
@total_size = with_file.stat.size if with_file.respond_to?(:stat)
+ @progress_block = blk.to_proc if blk
end
def each(sep_string = $/, &blk)
# Report offset at each call of the iterator
result = super(sep_string) do | line |
@@ -67,12 +67,9 @@
def pos=(p)
returning(super) { notify_read }
end
private
- def returning(r)
- yield; r
- end
def notify_read
@progress_block.call(pos, @total_size) if @progress_block
end
end
\ No newline at end of file