filter_io 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/filter_io.rb +14 -2
- data/lib/filter_io/version.rb +1 -1
- data/spec/filter_io_spec.rb +24 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb0cdede3401dcdd670bf293213c4836b1abd8b5
|
4
|
+
data.tar.gz: 6c9e0d608afff977d2acf2688e5a16c2357664f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d63ff2053219b2abca0dcaecb876550fbfcaf3b09c657c0acb758d5eaed34f601ef059d647c10caa11b3de2549fd20eeeb846e2f9b8c0f375332da6f12d2d84
|
7
|
+
data.tar.gz: e80aeeb45c363e80a62a8c831a5fdfb8b94473ae9a3b98fe6ace97abaea623cff3380ec54c7333738450581020c788de8de8a6ffb90a46e848fda679da8e00a0
|
data/lib/filter_io.rb
CHANGED
@@ -18,6 +18,7 @@ class FilterIO
|
|
18
18
|
@io = io
|
19
19
|
@options = options || {}
|
20
20
|
@block = block
|
21
|
+
@source_pos = 0
|
21
22
|
@pos = 0
|
22
23
|
@buffer = empty_string
|
23
24
|
@buffer_raw = empty_string_raw
|
@@ -152,6 +153,7 @@ class FilterIO
|
|
152
153
|
# noop
|
153
154
|
when 0
|
154
155
|
@io.rewind
|
156
|
+
@source_pos = 0
|
155
157
|
@pos = 0
|
156
158
|
@buffer = empty_string
|
157
159
|
@buffer_raw = empty_string_raw
|
@@ -305,6 +307,7 @@ class FilterIO
|
|
305
307
|
if !@buffer_raw.empty?
|
306
308
|
data = @buffer_raw.slice! 0, @buffer_raw.bytesize
|
307
309
|
elsif data = @io.read(block_size)
|
310
|
+
@source_pos += data.bytesize
|
308
311
|
data.force_encoding(external_encoding)
|
309
312
|
else
|
310
313
|
return
|
@@ -323,7 +326,9 @@ class FilterIO
|
|
323
326
|
end
|
324
327
|
rescue NeedMoreData => e
|
325
328
|
raise EOFError, 'end of file reached' if @io.eof?
|
326
|
-
|
329
|
+
new_data = @io.read(block_size).force_encoding(external_encoding)
|
330
|
+
data << new_data
|
331
|
+
@source_pos += new_data.bytesize
|
327
332
|
retry
|
328
333
|
end
|
329
334
|
|
@@ -355,7 +360,14 @@ class FilterIO
|
|
355
360
|
|
356
361
|
if data && @block
|
357
362
|
args = [data.dup]
|
358
|
-
|
363
|
+
if @block.arity > 1
|
364
|
+
src_pos = begin
|
365
|
+
@io.pos
|
366
|
+
rescue Errno::ESPIPE
|
367
|
+
@source_pos
|
368
|
+
end
|
369
|
+
args << BlockState.new(src_pos == data.length, source_eof?)
|
370
|
+
end
|
359
371
|
data = @block.call(*args)
|
360
372
|
raise IOError, 'Block returned nil' if data.nil?
|
361
373
|
end
|
data/lib/filter_io/version.rb
CHANGED
data/spec/filter_io_spec.rb
CHANGED
@@ -348,6 +348,18 @@ describe FilterIO do
|
|
348
348
|
expect(io.read).to eq expected
|
349
349
|
end
|
350
350
|
|
351
|
+
it 'passes false for BOF to the block if stream previously read' do
|
352
|
+
input = StringIO.new 'Test String'
|
353
|
+
expect(input.read(4)).to eq 'Test'
|
354
|
+
io = FilterIO.new(input, :block_size => 4) do |data, state|
|
355
|
+
data = "*#{data}*"
|
356
|
+
data = ">>>#{data}" if state.bof?
|
357
|
+
data = "#{data}<<<" if state.eof?
|
358
|
+
data
|
359
|
+
end
|
360
|
+
expect(io.read).to eq '* Str**ing*<<<'
|
361
|
+
end
|
362
|
+
|
351
363
|
it 'passes a copy of the data to block (to prevent mutation bugs)' do
|
352
364
|
input = "foobar"
|
353
365
|
expected = [
|
@@ -865,6 +877,18 @@ describe FilterIO do
|
|
865
877
|
expect(io.read).to eq 'TEST'
|
866
878
|
end
|
867
879
|
|
880
|
+
it 'supports filtering from a pipe with state' do
|
881
|
+
read_io, write_io = IO::pipe
|
882
|
+
write_io.write 'test'
|
883
|
+
write_io.close
|
884
|
+
io = FilterIO.new read_io do |data, state|
|
885
|
+
out = state.bof? ? 'start' : ''
|
886
|
+
out << data.upcase
|
887
|
+
out << 'end' if state.eof?
|
888
|
+
end
|
889
|
+
expect(io.read).to eq 'startTESTend'
|
890
|
+
end
|
891
|
+
|
868
892
|
it 'supports IO.copy_stream' do
|
869
893
|
input = StringIO.new "Test"
|
870
894
|
output = StringIO.new
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filter_io
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Weathered
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|