Class: ProgressiveIO
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- ProgressiveIO
- Defined in:
- lib/progressive_io.rb
Overview
A wrapper class that provides progress tracking for IO operations.
This class wraps an IO object and calls a progress block whenever data is read, allowing you to track reading progress for operations like file uploads or downloads.
Constant Summary collapse
- VERSION =
The version of the ProgressiveIO library
'2.0.1'
Instance Attribute Summary collapse
-
#progress_block ⇒ Proc?
The progress callback block that will be called when data is read The block receives one parameter: current position.
Instance Method Summary collapse
-
#each(sep_string = $/) {|line| ... } ⇒ Enumerator
(also: #each_line)
Iterates over the IO stream line by line, calling the progress block for each line.
-
#each_byte {|byte| ... } ⇒ Enumerator
Iterates over the IO stream byte by byte, calling the progress block for each byte.
-
#getc ⇒ String?
Reads a single character from the IO stream.
-
#gets(*args) ⇒ String?
Reads a line from the IO stream.
-
#initialize(with_io, &blk) {|current_pos| ... } ⇒ ProgressiveIO
constructor
Creates a new ProgressiveIO wrapper around an IO object.
-
#pos=(p) ⇒ Integer
Sets the position in the IO stream.
-
#read(*a) ⇒ String?
Reads data from the IO stream.
-
#readbytes(*a) ⇒ String
Reads a specific number of bytes from the IO stream.
-
#readchar ⇒ String
Reads a single character from the IO stream.
-
#readline(*a) ⇒ String
Reads a line from the IO stream.
-
#readlines(*a) ⇒ Array<String>
Reads all lines from the IO stream.
-
#seek(*a) ⇒ Integer
Seeks to a position in the IO stream.
Constructor Details
#initialize(with_io, &blk) {|current_pos| ... } ⇒ ProgressiveIO
Creates a new ProgressiveIO wrapper around an IO object.
50 51 52 53 |
# File 'lib/progressive_io.rb', line 50 def initialize(with_io, &blk) super(with_io) @progress_block = blk.to_proc if blk end |
Instance Attribute Details
#progress_block ⇒ Proc?
Returns The progress callback block that will be called when data is read The block receives one parameter: current position.
35 36 37 |
# File 'lib/progressive_io.rb', line 35 def progress_block @progress_block end |
Instance Method Details
#each(sep_string = $/) {|line| ... } ⇒ Enumerator Also known as: each_line
Iterates over the IO stream line by line, calling the progress block for each line.
66 67 68 69 70 71 |
# File 'lib/progressive_io.rb', line 66 def each(sep_string = $/, &blk) # Report offset at each call of the iterator super(sep_string) do |line| yield(line).tap { notify_read } end end |
#each_byte {|byte| ... } ⇒ Enumerator
Iterates over the IO stream byte by byte, calling the progress block for each byte.
84 85 86 87 |
# File 'lib/progressive_io.rb', line 84 def each_byte(&blk) # Report offset at each call of the iterator super { |b| yield(b).tap { notify_read } } end |
#getc ⇒ String?
Reads a single character from the IO stream.
93 94 95 |
# File 'lib/progressive_io.rb', line 93 def getc super.tap { notify_read } end |
#gets(*args) ⇒ String?
Reads a line from the IO stream.
102 103 104 |
# File 'lib/progressive_io.rb', line 102 def gets(*args) super(*args).tap { notify_read } end |
#pos=(p) ⇒ Integer
Sets the position in the IO stream.
170 171 172 |
# File 'lib/progressive_io.rb', line 170 def pos=(p) super(p).tap { notify_read } end |
#read(*a) ⇒ String?
Reads data from the IO stream.
111 112 113 |
# File 'lib/progressive_io.rb', line 111 def read(*a) super(*a).tap { notify_read } end |
#readbytes(*a) ⇒ String
Reads a specific number of bytes from the IO stream.
120 121 122 |
# File 'lib/progressive_io.rb', line 120 def readbytes(*a) super(*a).tap { notify_read } end |
#readchar ⇒ String
Reads a single character from the IO stream.
129 130 131 |
# File 'lib/progressive_io.rb', line 129 def readchar super.tap { notify_read } end |
#readline(*a) ⇒ String
Reads a line from the IO stream.
139 140 141 |
# File 'lib/progressive_io.rb', line 139 def readline(*a) super(*a).tap { notify_read } end |
#readlines(*a) ⇒ Array<String>
Reads all lines from the IO stream.
148 149 150 |
# File 'lib/progressive_io.rb', line 148 def readlines(*a) super(*a).tap { notify_read } end |
#seek(*a) ⇒ Integer
Seeks to a position in the IO stream.
157 158 159 |
# File 'lib/progressive_io.rb', line 157 def seek(*a) super(*a) end |