Permalink
Browse files

add Tempfile.delete_all method to delete all files generated by Tempf…

…ile class
  • Loading branch information...
1 parent c541ee1 commit ef45de4dbfb924a022f6df32b0b127924057fb30 @ak-mochi ak-mochi committed Aug 27, 2013
Showing with 18 additions and 0 deletions.
  1. +18 −0 mrblib/tempfile.rb
View
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
class Tempfile < File
+ @@tempfiles = []
+
def initialize(basename, tempdir = Dir::tmpdir)
self._bless
@deleted = false
@@ -10,6 +12,10 @@ def initialize(basename, tempdir = Dir::tmpdir)
@path = @path || make_tmpname(basename, tempdir)
super(@path, @mode, @perm)
+
+ @@tempfiles << self
+
+ self
end
def self.open(basename, tempdir = Dir::tmpdir, &block)
@@ -28,6 +34,17 @@ def self.open(basename, tempdir = Dir::tmpdir, &block)
end
+ def self.delete_all
+ while not @@tempfiles.empty?
+ @@tempfiles.shift.delete
+ end
+
+ end
+
+ def self.tempfiles
+ @@tempfiles
+ end
+
def make_tmpname(basename, tempdir, n=nil)
rand_max = 0x100000000
t = Time.now
@@ -87,6 +104,7 @@ def close!
def delete
File.delete(@path)
@deleted = true
+ @@tempfiles.delete(self)
self
end

0 comments on commit ef45de4

Please sign in to comment.