Skip to content

Commit fcce0c2

Browse files
luislavenaysbaddaden
authored andcommittedAug 28, 2017
Normalize Flate's block interface to .open
Adjust both `Flate::Reader` and `Flate::Writer` interfaces for block method `.open`, offering auto-close of reader/writer instance, respectively. This follows the interface defined by `Gzip::Reader` and `Gzip::Writer`, ensuring a uniform API for these compression methods. Add specs to ensure these block methods are covered and adjust documentation to highlight `io` parameter. Closes #4630
1 parent e844983 commit fcce0c2

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed
 

‎spec/std/flate/flate_spec.cr

+26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ module Flate
2020

2121
str.should eq("line1111\nline2222\n")
2222
end
23+
24+
describe ".open" do
25+
it "yields itself to block" do
26+
# Hello Crystal!
27+
message = Bytes[243, 72, 205, 201, 201, 87, 112, 46, 170, 44, 46, 73,
28+
204, 81, 4, 0]
29+
30+
io = IO::Memory.new(message)
31+
Reader.open(io) do |reader|
32+
reader.gets_to_end.should eq("Hello Crystal!")
33+
end
34+
end
35+
end
2336
end
2437

2538
describe Writer do
@@ -63,5 +76,18 @@ module Flate
6376
writer.closed?.should be_true
6477
io.closed?.should be_true
6578
end
79+
80+
describe ".open" do
81+
it "yields itself to block" do
82+
io = IO::Memory.new
83+
Writer.open(io) do |writer|
84+
writer.write "Hello Crystal!".to_slice
85+
end
86+
87+
io.rewind
88+
io.to_slice.should eq(Bytes[243, 72, 205, 201, 201, 87, 112, 46, 170, 44, 46, 73,
89+
204, 81, 4, 0])
90+
end
91+
end
6692
end
6793
end

‎src/flate/reader.cr

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ class Flate::Reader
2929
@end = false
3030
end
3131

32-
# Creates an instance of Flate::Reader, yields it to the given block, and closes
33-
# it at its end.
34-
def self.new(input : IO, sync_close : Bool = false, dict : Bytes? = nil)
35-
reader = new input, sync_close: sync_close, dict: dict
32+
# Creates a new reader from the given *io*, yields it to the given block,
33+
# and closes it at its end.
34+
def self.open(io : IO, sync_close : Bool = false, dict : Bytes? = nil)
35+
reader = new(io, sync_close: sync_close, dict: dict)
3636
yield reader ensure reader.close
3737
end
3838

‎src/flate/writer.cr

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ class Flate::Writer
3232
end
3333
end
3434

35-
# Creates an instance of Flate::Writer, yields it to the given block, and closes
36-
# it at its end.
37-
def self.new(output : IO, level : Int32 = Flate::DEFAULT_COMPRESSION,
38-
strategy : Flate::Strategy = Flate::Strategy::DEFAULT,
39-
sync_close : Bool = false, dict : Bytes? = nil)
40-
writer = new(output, level: level, strategy: strategy, sync_close: sync_close, dict: dict)
35+
# Creates a new writer for the given *io*, yields it to the given block,
36+
# and closes it at its end.
37+
def self.open(io : IO, level : Int32 = Flate::DEFAULT_COMPRESSION,
38+
strategy : Flate::Strategy = Flate::Strategy::DEFAULT,
39+
sync_close : Bool = false, dict : Bytes? = nil)
40+
writer = new(io, level: level, strategy: strategy, sync_close: sync_close, dict: dict)
4141
yield writer ensure writer.close
4242
end
4343

0 commit comments

Comments
 (0)
Please sign in to comment.