Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/file/path-temp.rb

Class Method Summary collapse

Class Method Details

.path_temp(opts = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/file/path-temp.rb', line 2

def self.path_temp(opts={})
  opts = {'delete'=>true}.merge(opts)
  
  # root
  if opts['root']
    root = opts['root'].sub(/\/*\z/mu, '/')
  else
    root = './'
  end
  
  # full path
  path = rand.to_s
  path = path.sub(/\A0\./mu, '')
  path = root + path
  
  # add extension
  if opts['ext']
    ext = opts['ext']
    ext = ext.sub(/\A\.*/mu, '.')
    path += ext
  end
  
  begin
    yield path
  ensure
    if opts['delete'] and File.exist?(path)
      File.delete path
    end
  end
end