Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* Result block value for SevenZipReader.open & .open_file #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/seven_zip_ruby/seven_zip_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ class << self
# SevenZipRuby::Reader.open(stream) do |szr|
# szr.extract(:all, "path_to_dir")
# end
def open(stream, param = {}, &block) # :yield: szr
def open(stream, param = {}) # :yield: szr
szr = self.new
szr.open(stream, param)
if (block)
# Using `block_given?`, `yield` & omitting `&block`
# is faster
# Ref: https://github.com/JuanitoFatas/fast-ruby#proccall-and-block-arguments-vs-yieldcode
if block_given?
begin
block.call(szr)
szr.close
yield(szr).tap do
szr.close
end
ensure
szr.close_file
end
Expand Down Expand Up @@ -135,13 +139,17 @@ def open(stream, param = {}, &block) # :yield: szr
# szr = SevenZipRuby::SevenZipReader.open_file("filename.7z")
# # Read and extract archive.
# szr.close
def open_file(filename, param = {}, &block) # :yield: szr
def open_file(filename, param = {}) # :yield: szr
szr = self.new
szr.open_file(filename, param)
if (block)
# Using `block_given?`, `yield` & omitting `&block`
# is faster
# Ref: https://github.com/JuanitoFatas/fast-ruby#proccall-and-block-arguments-vs-yieldcode
if block_given?
begin
block.call(szr)
szr.close
yield(szr).tap do
szr.close
end
ensure
szr.close_file
end
Expand Down
34 changes: 34 additions & 0 deletions spec/seven_zip_ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@
end
end

describe "singleton method: open" do
context "called with block" do
let(:block_value) { "Block value" }

subject(:returned_result) do
File.open(SevenZipRubySpecHelper::SEVEN_ZIP_FILE, "rb") do |file|
SevenZipRuby::SevenZipReader.open(file) do |_szr|
block_value
end
end
end

it "returns block value" do
should eq(block_value)
end
end
end

describe "singleton method: open_file" do
context "called with block" do
let(:block_value) { "Block value" }

subject(:returned_result) do
SevenZipRuby::SevenZipReader.open_file(SevenZipRubySpecHelper::SEVEN_ZIP_FILE) do |_szr|
block_value
end
end

it "returns block value" do
should eq(block_value)
end
end
end

example "extract archive" do
File.open(SevenZipRubySpecHelper::SEVEN_ZIP_FILE, "rb") do |file|
SevenZipRuby::SevenZipReader.open(file) do |szr|
Expand Down