Module: FileUtils::TmpFile
- Defined in:
- lib/vex/base/filesystem/tmp_file.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#tmpfile(path = nil, &block) ⇒ Object
tmpfile(“xx.jpg”) do |dest| Net.download(“”, dest) end.
Class Method Details
Instance Method Details
#tmpfile(path = nil, &block) ⇒ Object
tmpfile(“xx.jpg”) do |dest|
Net.download("http://xx.yy.zz/a.jpg", dest)
end
the block gets the temp file name, which is guaranteed to be unique amongst all running processes.
If the path parameter is set the temporary file will be fastcopied to that output file.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/vex/base/filesystem/tmp_file.rb', line 17 def tmpfile(path=nil, &block) raise ArgumentError, "This no longer supports Symbol parameters" if path.is_a?(Symbol) ext = "#{Thread.uid}_#{FileUtils::TmpFile.counter}" case path when nil tmp = "#{App.tmpdir}/data.#{ext}" else Dir.mkdirs(File.dirname(path)) tmp = "#{path}.tmp#{ext}" end begin result = yield(tmp) if result != false && path && File.exist?(tmp) FileUtils.fast_copy(tmp, path) end return result ensure File.unlink(tmp) if File.exists?(tmp) end end |