Skip to content

Commit

Permalink
Fix some indenting and coding style inconsistencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
FooBarWidget committed Aug 20, 2009
1 parent c294044 commit ff0c8c5
Showing 1 changed file with 40 additions and 41 deletions.
81 changes: 40 additions & 41 deletions lib/better/tempfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def initialize(basename, *rest)

lock = tmpname = nil
n = failure = 0
@@lock.synchronize {
@@lock.synchronize do
begin
begin
tmpname = File.join(tmpdir, make_tmpname(basename, n))
Expand All @@ -125,7 +125,7 @@ def initialize(basename, *rest)
retry if failure < MAX_TRY
raise "cannot generate tempfile `%s'" % tmpname
end
}
end

@data = [tmpname]
@clean_proc = self.class.callback(@data)
Expand All @@ -149,35 +149,15 @@ def initialize(basename, *rest)

Dir.rmdir(lock)
end

def make_tmpname(basename, n)
case basename
when Array
prefix, suffix = *basename
else
prefix, suffix = basename, ''
end

t = Time.now.strftime("%Y%m%d")
path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}"
end
private :make_tmpname


# Opens or reopens the file with mode "r+".
def open
@tmpfile.close if @tmpfile
@tmpfile = File.open(@tmpname, 'r+')
@data[1] = @tmpfile
__setobj__(@tmpfile)
end

def _close # :nodoc:
@tmpfile.close if @tmpfile
@tmpfile = nil
@data[1] = nil if @data
end
protected :_close


#Closes the file. If the optional flag is true, unlinks the file
# after closing.
#
Expand Down Expand Up @@ -272,21 +252,21 @@ def size
class << self
def callback(data) # :nodoc:
pid = $$
Proc.new {
if pid == $$
path, tmpfile, cleanlist = *data
Proc.new do
if pid == $$
path, tmpfile, cleanlist = *data

print "removing ", path, "..." if $DEBUG
print "removing ", path, "..." if $DEBUG

tmpfile.close if tmpfile
tmpfile.close if tmpfile

# keep this order for thread safeness
File.unlink(path) if File.exist?(path)
cleanlist.delete(path) if cleanlist
# keep this order for thread safeness
File.unlink(path) if File.exist?(path)
cleanlist.delete(path) if cleanlist

print "done\n" if $DEBUG
end
}
print "done\n" if $DEBUG
end
end
end

# If no block is given, this is a synonym for new().
Expand All @@ -298,21 +278,40 @@ def open(*args)
tempfile = new(*args)

if block_given?
begin
yield(tempfile)
ensure
tempfile.close
end
begin
yield(tempfile)
ensure
tempfile.close
end
else
tempfile
tempfile
end
end
end

protected
def _close # :nodoc:
@tmpfile.close if @tmpfile
@tmpfile = nil
@data[1] = nil if @data
end

private
def unlink_file(filename)
File.unlink(filename)
end

def make_tmpname(basename, n)
case basename
when Array
prefix, suffix = *basename
else
prefix, suffix = basename, ''
end

t = Time.now.strftime("%Y%m%d")
path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}"
end
end

end # module Better

0 comments on commit ff0c8c5

Please sign in to comment.