progressive_io 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/progressive_io.rb +18 -31
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea6428465be21d680077d7ed5077c9e8e8f0f72622249e3ccb2f585f38e7d969
|
4
|
+
data.tar.gz: f71c4577f2642efb23c3df42697cc52ce08a7cca603af89825efaf5c760c32b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad62723654f1655d14b3a6b391581b1adcef7dbe27bf9a05901f405703118cb251b1bd4b4639ad3ce81d6099ba031596402ce16b96a54476ecdb2ddfa90cdba0
|
7
|
+
data.tar.gz: c1b87a837cb5f2a10defbf2dd1ec493b90b42db58f717b1aca2dfc6827af134fd223d4ca91e307ece53f0560e843fefc02b077f3cd72db47eed6e286f9a927e9
|
data/CHANGELOG.md
CHANGED
data/lib/progressive_io.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'delegate'
|
4
|
+
|
3
5
|
# A wrapper class that provides progress tracking for IO operations.
|
4
6
|
#
|
5
7
|
# This class wraps an IO object and calls a progress block whenever data is read,
|
@@ -24,9 +26,9 @@
|
|
24
26
|
# content = progress_io.read
|
25
27
|
#
|
26
28
|
# @since 2.0.0
|
27
|
-
class ProgressiveIO
|
29
|
+
class ProgressiveIO < SimpleDelegator
|
28
30
|
# The version of the ProgressiveIO library
|
29
|
-
VERSION = '2.0.
|
31
|
+
VERSION = '2.0.1'
|
30
32
|
|
31
33
|
# @return [Proc, nil] The progress callback block that will be called when data is read
|
32
34
|
# The block receives one parameter: current position
|
@@ -46,7 +48,7 @@ class ProgressiveIO
|
|
46
48
|
# puts "Read #{pos} bytes"
|
47
49
|
# end
|
48
50
|
def initialize(with_io, &blk)
|
49
|
-
|
51
|
+
super(with_io)
|
50
52
|
@progress_block = blk.to_proc if blk
|
51
53
|
end
|
52
54
|
|
@@ -63,7 +65,7 @@ class ProgressiveIO
|
|
63
65
|
# end
|
64
66
|
def each(sep_string = $/, &blk)
|
65
67
|
# Report offset at each call of the iterator
|
66
|
-
|
68
|
+
super(sep_string) do |line|
|
67
69
|
yield(line).tap { notify_read }
|
68
70
|
end
|
69
71
|
end
|
@@ -81,7 +83,7 @@ class ProgressiveIO
|
|
81
83
|
# end
|
82
84
|
def each_byte(&blk)
|
83
85
|
# Report offset at each call of the iterator
|
84
|
-
|
86
|
+
super { |b| yield(b).tap { notify_read } }
|
85
87
|
end
|
86
88
|
|
87
89
|
# Reads a single character from the IO stream.
|
@@ -89,7 +91,7 @@ class ProgressiveIO
|
|
89
91
|
# @return [String, nil] The next character or nil if at end of stream
|
90
92
|
# @see IO#getc
|
91
93
|
def getc
|
92
|
-
|
94
|
+
super.tap { notify_read }
|
93
95
|
end
|
94
96
|
|
95
97
|
# Reads a line from the IO stream.
|
@@ -98,7 +100,7 @@ class ProgressiveIO
|
|
98
100
|
# @return [String, nil] The next line or nil if at end of stream
|
99
101
|
# @see IO#gets
|
100
102
|
def gets(*args)
|
101
|
-
|
103
|
+
super(*args).tap { notify_read }
|
102
104
|
end
|
103
105
|
|
104
106
|
# Reads data from the IO stream.
|
@@ -107,7 +109,7 @@ class ProgressiveIO
|
|
107
109
|
# @return [String, nil] The read data or nil if at end of stream
|
108
110
|
# @see IO#read
|
109
111
|
def read(*a)
|
110
|
-
|
112
|
+
super(*a).tap { notify_read }
|
111
113
|
end
|
112
114
|
|
113
115
|
# Reads a specific number of bytes from the IO stream.
|
@@ -116,7 +118,7 @@ class ProgressiveIO
|
|
116
118
|
# @return [String] The read bytes
|
117
119
|
# @see IO#readbytes
|
118
120
|
def readbytes(*a)
|
119
|
-
|
121
|
+
super(*a).tap { notify_read }
|
120
122
|
end
|
121
123
|
|
122
124
|
# Reads a single character from the IO stream.
|
@@ -125,7 +127,7 @@ class ProgressiveIO
|
|
125
127
|
# @raise [EOFError] If at end of stream
|
126
128
|
# @see IO#readchar
|
127
129
|
def readchar
|
128
|
-
|
130
|
+
super.tap { notify_read }
|
129
131
|
end
|
130
132
|
|
131
133
|
# Reads a line from the IO stream.
|
@@ -135,7 +137,7 @@ class ProgressiveIO
|
|
135
137
|
# @raise [EOFError] If at end of stream
|
136
138
|
# @see IO#readline
|
137
139
|
def readline(*a)
|
138
|
-
|
140
|
+
super(*a).tap { notify_read }
|
139
141
|
end
|
140
142
|
|
141
143
|
# Reads all lines from the IO stream.
|
@@ -144,7 +146,7 @@ class ProgressiveIO
|
|
144
146
|
# @return [Array<String>] Array of lines
|
145
147
|
# @see IO#readlines
|
146
148
|
def readlines(*a)
|
147
|
-
|
149
|
+
super(*a).tap { notify_read }
|
148
150
|
end
|
149
151
|
|
150
152
|
# Seeks to a position in the IO stream.
|
@@ -153,11 +155,11 @@ class ProgressiveIO
|
|
153
155
|
# @return [Integer] The new position
|
154
156
|
# @see IO#seek
|
155
157
|
def seek(*a)
|
156
|
-
|
158
|
+
super(*a)
|
157
159
|
end
|
158
160
|
|
159
161
|
# def ungetc(*a)
|
160
|
-
#
|
162
|
+
# super(*a).tap { notify_read }
|
161
163
|
# end
|
162
164
|
|
163
165
|
# Sets the position in the IO stream.
|
@@ -166,31 +168,16 @@ class ProgressiveIO
|
|
166
168
|
# @return [Integer] The new position
|
167
169
|
# @see IO#pos=
|
168
170
|
def pos=(p)
|
169
|
-
|
171
|
+
super(p).tap { notify_read }
|
170
172
|
end
|
171
173
|
|
172
174
|
private
|
173
175
|
|
174
|
-
# @return [IO] The wrapped IO object
|
175
|
-
def io
|
176
|
-
@io
|
177
|
-
end
|
178
|
-
|
179
|
-
# Delegates method calls to the wrapped IO object and calls the progress block.
|
180
|
-
#
|
181
|
-
# @param m [Symbol] The method name to call
|
182
|
-
# @param args [Array] Arguments to pass to the method
|
183
|
-
# @return [Object] The result of the method call
|
184
|
-
def inner(m, *args)
|
185
|
-
r = @io.respond_to?(:public_send) ? @io.public_send(m, *args) : @io.send(m, *args)
|
186
|
-
r.tap { notify_read }
|
187
|
-
end
|
188
|
-
|
189
176
|
# Calls the progress block with current position.
|
190
177
|
# This method is called whenever data is read from the IO stream.
|
191
178
|
#
|
192
179
|
# @return [void]
|
193
180
|
def notify_read
|
194
|
-
@progress_block.call(
|
181
|
+
@progress_block.call(pos) if @progress_block
|
195
182
|
end
|
196
183
|
end
|