Module: TmpFile
- Defined in:
- lib/rbbt/util/tmpfile.rb
Constant Summary collapse
- TMPDIR =
"/tmp/tmpfiles"
Class Method Summary collapse
-
.random_name(s = "", max = 10000000) ⇒ Object
Creates a random file name, with the given suffix and a random number up to
max
. -
.tmp_file(s = "", max = 10000000) ⇒ Object
Creates a random filename in the temporary directory.
- .tmpdir ⇒ Object
- .tmpdir=(tmpdir) ⇒ Object
- .with_dir(erase = true, options = {}) ⇒ Object
- .with_file(content = nil, erase = true, options = {}) ⇒ Object
Class Method Details
.random_name(s = "", max = 10000000) ⇒ Object
Creates a random file name, with the given suffix and a random number up to max
20 21 22 23 24 |
# File 'lib/rbbt/util/tmpfile.rb', line 20 def self.random_name(s = "", max = 10000000) n = rand(max) s << n.to_s s end |
.tmp_file(s = "", max = 10000000) ⇒ Object
Creates a random filename in the temporary directory
27 28 29 |
# File 'lib/rbbt/util/tmpfile.rb', line 27 def self.tmp_file(s = "",max=10000000) File.join(TMPDIR, random_name(s,max)) end |
.tmpdir ⇒ Object
13 14 15 |
# File 'lib/rbbt/util/tmpfile.rb', line 13 def self.tmpdir TMPDIR end |
.tmpdir=(tmpdir) ⇒ Object
8 9 10 11 |
# File 'lib/rbbt/util/tmpfile.rb', line 8 def self.tmpdir=(tmpdir) TMPDIR.replace tmpdir FileUtils.mkdir TMPDIR unless File.exist? TMPDIR end |
.with_dir(erase = true, options = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rbbt/util/tmpfile.rb', line 46 def self.with_dir(erase = true, = {}) tmpdir = tmp_file FileUtils.mkdir_p tmpdir result = yield(tmpdir) FileUtils.rm_rf tmpdir if File.exists?(tmpdir) and erase result end |
.with_file(content = nil, erase = true, options = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rbbt/util/tmpfile.rb', line 31 def self.with_file(content = nil, erase = true, = {}) tmpfile = tmp_file if [:extension] tmpfile += ".#{[:extension]}" end File.open(tmpfile, 'w') do |f| f.write content end if content != nil result = yield(tmpfile) FileUtils.rm_rf tmpfile if File.exists?(tmpfile) and erase result end |