Skip to content

Commit

Permalink
Normalize Flate's block interface to .open
Browse files Browse the repository at this point in the history
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
  • Loading branch information
luislavena authored and ysbaddaden committed Aug 28, 2017
1 parent e844983 commit fcce0c2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
26 changes: 26 additions & 0 deletions spec/std/flate/flate_spec.cr
Expand Up @@ -20,6 +20,19 @@ module Flate

str.should eq("line1111\nline2222\n")
end

describe ".open" do
it "yields itself to block" do
# Hello Crystal!
message = Bytes[243, 72, 205, 201, 201, 87, 112, 46, 170, 44, 46, 73,
204, 81, 4, 0]

io = IO::Memory.new(message)
Reader.open(io) do |reader|
reader.gets_to_end.should eq("Hello Crystal!")
end
end
end
end

describe Writer do
Expand Down Expand Up @@ -63,5 +76,18 @@ module Flate
writer.closed?.should be_true
io.closed?.should be_true
end

describe ".open" do
it "yields itself to block" do
io = IO::Memory.new
Writer.open(io) do |writer|
writer.write "Hello Crystal!".to_slice
end

io.rewind
io.to_slice.should eq(Bytes[243, 72, 205, 201, 201, 87, 112, 46, 170, 44, 46, 73,
204, 81, 4, 0])
end
end
end
end
8 changes: 4 additions & 4 deletions src/flate/reader.cr
Expand Up @@ -29,10 +29,10 @@ class Flate::Reader
@end = false
end

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

Expand Down
12 changes: 6 additions & 6 deletions src/flate/writer.cr
Expand Up @@ -32,12 +32,12 @@ class Flate::Writer
end
end

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

Expand Down

0 comments on commit fcce0c2

Please sign in to comment.