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.2'
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(*args) {|line| ... } ⇒ Enumerator
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.
-
#each_line(*args) {|line| ... } ⇒ Enumerator
Iterates over the IO stream line by line, calling the progress block for each line.
-
#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(*args) {|line| ... } ⇒ Enumerator
Iterates over the IO stream line by line, calling the progress block for each line.
71 72 73 74 75 76 77 78 |
# File 'lib/progressive_io.rb', line 71 def each(*args, &blk) return enum_for(__method__, *args) unless block_given? # Report offset at each call of the iterator super(*args) 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.
118 119 120 121 122 123 |
# File 'lib/progressive_io.rb', line 118 def each_byte(&blk) return enum_for(__method__) unless block_given? # Report offset at each call of the iterator super { |b| yield(b).tap { notify_read } } end |
#each_line(*args) {|line| ... } ⇒ Enumerator
Iterates over the IO stream line by line, calling the progress block for each line. This is an alias-like method for #each that ensures proper Enumerator behavior.
97 98 99 100 101 |
# File 'lib/progressive_io.rb', line 97 def each_line(*args, &blk) return enum_for(__method__, *args) unless block_given? each(*args, &blk) end |
#getc ⇒ String?
Reads a single character from the IO stream.
129 130 131 |
# File 'lib/progressive_io.rb', line 129 def getc super.tap { notify_read } end |
#gets(*args) ⇒ String?
Reads a line from the IO stream.
138 139 140 |
# File 'lib/progressive_io.rb', line 138 def gets(*args) super(*args).tap { notify_read } end |
#pos=(p) ⇒ Integer
Sets the position in the IO stream.
206 207 208 |
# File 'lib/progressive_io.rb', line 206 def pos=(p) super(p).tap { notify_read } end |
#read(*a) ⇒ String?
Reads data from the IO stream.
147 148 149 |
# File 'lib/progressive_io.rb', line 147 def read(*a) super(*a).tap { notify_read } end |
#readbytes(*a) ⇒ String
Reads a specific number of bytes from the IO stream.
156 157 158 |
# File 'lib/progressive_io.rb', line 156 def readbytes(*a) super(*a).tap { notify_read } end |
#readchar ⇒ String
Reads a single character from the IO stream.
165 166 167 |
# File 'lib/progressive_io.rb', line 165 def readchar super.tap { notify_read } end |
#readline(*a) ⇒ String
Reads a line from the IO stream.
175 176 177 |
# File 'lib/progressive_io.rb', line 175 def readline(*a) super(*a).tap { notify_read } end |
#readlines(*a) ⇒ Array<String>
Reads all lines from the IO stream.
184 185 186 |
# File 'lib/progressive_io.rb', line 184 def readlines(*a) super(*a).tap { notify_read } end |
#seek(*a) ⇒ Integer
Seeks to a position in the IO stream.
193 194 195 |
# File 'lib/progressive_io.rb', line 193 def seek(*a) super(*a) end |