summaryrefslogtreecommitdiff
path: root/lib/tempfile.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tempfile.rb')
-rw-r--r--lib/tempfile.rb36
1 files changed, 10 insertions, 26 deletions
diff --git a/lib/tempfile.rb b/lib/tempfile.rb
index f0231bf..0d3c2a3 100644
--- a/lib/tempfile.rb
+++ b/lib/tempfile.rb
@@ -78,8 +78,6 @@ require 'tmpdir'
# same Tempfile object from multiple threads then you should protect it with a
# mutex.
class Tempfile < DelegateClass(File)
- include Dir::Tmpname
-
# call-seq:
# new(basename, [tmpdir = Dir.tmpdir], [options])
#
@@ -124,7 +122,7 @@ class Tempfile < DelegateClass(File)
#
# If Tempfile.new cannot find a unique filename within a limited
# number of tries, then it will raise an exception.
- def initialize(basename, *rest)
+ def initialize(basename, tmpdir=nil, mode: 0, **opts)
if block_given?
warn "Tempfile.new doesn't call the given block."
end
@@ -132,20 +130,13 @@ class Tempfile < DelegateClass(File)
@clean_proc = Remover.new(@data)
ObjectSpace.define_finalizer(self, @clean_proc)
- ::Dir::Tmpname.create(basename, *rest) do |tmpname, n, opts|
- mode = File::RDWR|File::CREAT|File::EXCL
- perm = 0600
- if opts
- mode |= opts.delete(:mode) || 0
- opts[:perm] = perm
- perm = nil
- else
- opts = perm
- end
+ ::Dir::Tmpname.create(basename, tmpdir, opts) do |tmpname, n, opts|
+ mode |= File::RDWR|File::CREAT|File::EXCL
+ opts[:perm] = 0600
@data[1] = @tmpfile = File.open(tmpname, mode, opts)
@data[0] = @tmpname = tmpname
@mode = mode & ~(File::CREAT|File::EXCL)
- perm or opts.freeze
+ opts.freeze
@opts = opts
end
@@ -278,7 +269,7 @@ class Tempfile < DelegateClass(File)
def call(*args)
return if @pid != $$
- path, tmpfile = *@data
+ path, tmpfile = @data
STDERR.print "removing ", path, "..." if $DEBUG
@@ -356,18 +347,11 @@ end
# ... do something with f ...
# end
#
-def Tempfile.create(basename, *rest)
+def Tempfile.create(basename, tmpdir=nil, mode: 0, **opts)
tmpfile = nil
- Dir::Tmpname.create(basename, *rest) do |tmpname, n, opts|
- mode = File::RDWR|File::CREAT|File::EXCL
- perm = 0600
- if opts
- mode |= opts.delete(:mode) || 0
- opts[:perm] = perm
- perm = nil
- else
- opts = perm
- end
+ Dir::Tmpname.create(basename, tmpdir, opts) do |tmpname, n, opts|
+ mode |= File::RDWR|File::CREAT|File::EXCL
+ opts[:perm] = 0600
tmpfile = File.open(tmpname, mode, opts)
end
if block_given?