Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08e96f2
commit fc7891e
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'nuggets/file/open_file_mixin' | ||
|
||
class File | ||
extend Nuggets::File::OpenFileMixin | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#-- | ||
############################################################################### | ||
# # | ||
# nuggets -- Extending Ruby # | ||
# # | ||
# Copyright (C) 2007-2015 Jens Wille # | ||
# # | ||
# Authors: # | ||
# Jens Wille <jens.wille@gmail.com> # | ||
# # | ||
# nuggets is free software; you can redistribute it and/or modify it under # | ||
# the terms of the GNU Affero General Public License as published by the Free # | ||
# Software Foundation; either version 3 of the License, or (at your option) # | ||
# any later version. # | ||
# # | ||
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY # | ||
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # | ||
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for # | ||
# more details. # | ||
# # | ||
# You should have received a copy of the GNU Affero General Public License # | ||
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. # | ||
# # | ||
############################################################################### | ||
#++ | ||
|
||
module Nuggets | ||
class File | ||
module OpenFileMixin | ||
|
||
# call-seq: | ||
# File.open_file(filename[, options[, mode]]) -> anIO | ||
# File.open_file(filename[, options[, mode]]) { |io| ... } => anObject | ||
# | ||
# Supported options are +mode+ (defaults to +r+) and +encoding+ (defaults | ||
# to the default external encoding). | ||
# | ||
# If +filename+ is +-+, sets +io+ to +STDOUT+ when in write mode or +STDIN+ | ||
# otherwise. Puts +io+ into binary mode if requested. Sets +io+'s encoding. | ||
# | ||
# If +filename+ ends with +.gz+ or +.gzip+, uses Zlib for reading or writing | ||
# the file. | ||
# | ||
# Otherwise defers to ::open. | ||
# | ||
# Yields +io+ to the given block or returns it when no block given. | ||
def open_file(filename, options = {}, mode = 'r', &block) | ||
mode = options.fetch(:mode, mode); writing = mode =~ /w/ | ||
|
||
encoding = options.fetch(:encoding, ::Encoding.default_external) | ||
|
||
case filename | ||
when '-' | ||
io = writing ? $stdout : $stdin | ||
io.binmode if mode =~ /b/ | ||
io.set_encoding(encoding) | ||
|
||
block ? block[io] : io | ||
when /\.gz(?:ip)?\z/i | ||
require 'zlib' | ||
|
||
klass = writing ? ::Zlib::GzipWriter : ::Zlib::GzipReader | ||
klass.open(filename, encoding: encoding, &block) | ||
else | ||
open(filename, mode, encoding: encoding, &block) | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |