Module: TempPath
- Defined in:
- lib/temp_path.rb
Constant Summary collapse
- @@initialized =
false
- @@mutex =
Mutex.new
Class Method Summary collapse
- .auto_clean=(aBool) ⇒ Object
-
.clean(&block) ⇒ Object
By default the autoclean is on.
-
.fork_init(sub_dir = true) ⇒ Object
Call TempPath.fork_init when you want to reset the global TempPath status.
-
.init(tmp_base_dir = nil) ⇒ Object
:nodoc:.
-
.new(base = nil, ext = '', &block) ⇒ Object
You can use a temporary pathname like that:.
- .tmpdir ⇒ Object
Class Method Details
.auto_clean=(aBool) ⇒ Object
128 129 130 |
# File 'lib/temp_path.rb', line 128 def auto_clean= ( aBool ) @@auto_clean = aBool end |
.clean(&block) ⇒ Object
By default the autoclean is on. But in some case (if you use your temppaths in at_exit) You must disable the autoclean. And manually call TempPath.clean at the very of the program.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/temp_path.rb', line 113 def clean ( &block ) @@mutex.synchronize do return if @@clean_planned @@clean_planned = true end begin block[] if block_given? ensure if @@tmpdir.exist? @@tmpdir.rmtree @@initialized = false end end end |
.fork_init(sub_dir = true) ⇒ Object
Call TempPath.fork_init when you want to reset the global TempPath status. For example when you make a fork and you don’t want that the child have and destroy the same temporary directory as the father. This method setup a child TempPath environement where the temporary directory of the child is in the father one (unless if you set sub_dir
to false)
103 104 105 106 107 |
# File 'lib/temp_path.rb', line 103 def fork_init ( sub_dir=true ) @@tmpdir ||= nil @@initialized = false init((sub_dir)? @@tmpdir : nil) end |
.init(tmp_base_dir = nil) ⇒ Object
:nodoc:
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/temp_path.rb', line 82 def init ( tmp_base_dir=nil ) #:nodoc: return if @@initialized @@mutex.synchronize do return if @@initialized @@tmps = Set.new @@progname = Pathname.new($0).basename @@tmpdir = (tmp_base_dir || Dir.tmpdir).to_path + "#{@@progname}.#{$$}" @@tmpdir.freeze @@clean_planned = false @@auto_clean = true @@tmpdir.mkpath @@initialized = true at_exit { clean if @@auto_clean } end end |
.new(base = nil, ext = '', &block) ⇒ Object
You can use a temporary pathname like that:
- # No argument are mandatory.
tmp = TempPath.new
tmp.open('w') { |f| f << 'foo' }
tmp.clean
- # You can choose the basename and an extension.
TempPath.new('the_base_file_name', 'rb')
- # You can supply a block (recomended).
TempPath.new('foo') do |tmp|
tmp.open('w') { |f| << 'foo' }
tmp.exist? == true
$tmp = tmp
end
$tmp.exist? == false
- # You can make a temporary directory.
TempPath.new('adirectory') do |tmpdir|
tmpdir.mkpath
$file = tmpdir + 'foo'
$file.open('w') { |f| f << 'foo' }
end
$file.exist? == false
The file name follow this scheme:
TempPath.new(‘foo’, ‘rb’)
=> 'foo.111811.432.rb'
TempPath.new(‘bar’)
=> 'bar.111811.134'
which follow this format:
=> 'base.pid.uniq.ext
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/temp_path.rb', line 54 def new base=nil, ext='', &block tmp = nil if base and base.to_s =~ /\// raise ArgumentError, "bad basename, you give me a pathname #{base}" end init base ||= @@progname ext = ".#{ext}" unless ext.empty? or ext[0] == ?. res = nil @@mutex.synchronize do tmp = Pathname.new '' id_tmp = tmp.object_id while (res = @@tmpdir + "#{base}.#{id_tmp}#{ext}").exist? \ and not @@tmps.include? res id_tmp += 1 end tmp.instance_eval { @path = res.instance_eval { @path } } tmp.extend TempPathname @@tmps << self end return tmp unless block_given? begin return block[tmp.dup] ensure tmp.clean end end |
.tmpdir ⇒ Object
132 133 134 135 |
# File 'lib/temp_path.rb', line 132 def tmpdir init @@tmpdir end |