Skip to content

Commit

Permalink
Add support for CompositeReadIO#close.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Feb 7, 2024
1 parent 062e35d commit c5677a0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/multipart/post/composite_read_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,26 @@ def initialize(*ios)
@index = 0
end

# Close all the underyling IOs.
def close
@ios.each do |io|
io.close if io.respond_to?(:close)
end

@ios = nil
@index = 0
end

def closed?
@ios.nil?
end

# Read from IOs in order until `length` bytes have been received.
def read(length = nil, outbuf = nil)
if @ios.nil?
raise IOError, "CompositeReadIO is closed!"
end

got_result = false
outbuf = outbuf ? outbuf.replace("") : String.new

Expand Down
5 changes: 5 additions & 0 deletions spec/multipart/post/composite_read_io_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
MULTIBYTE = File.dirname(__FILE__) + '/../../fixtures/multibyte.txt'

RSpec.shared_context "composite io" do
it "can close all the ios" do
subject.close
expect(subject.closed?).to be_truthy
end

it "test_full_read_from_several_ios" do
expect(subject.read).to be == 'the quick brown fox'
end
Expand Down

0 comments on commit c5677a0

Please sign in to comment.