Module: Dir::TmpdirBlock

Defined in:
lib/tmpdir_block.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'1.0.0'
Super =
Dir.send(:method, :tmpdir)
Hostname =
Socket.gethostname rescue 'localhost'
Pid =
Process.pid
Ppid =
Process.ppid

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.versionObject



4
# File 'lib/tmpdir_block.rb', line 4

def TmpdirBlock.version() TmpdirBlock::VERSION end

Instance Method Details

#tmpdir(*args, &block) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tmpdir_block.rb', line 23

def tmpdir(*args, &block)
  options = Hash === args.last ? args.pop : {}

  dirname = Super.call(*args)

  return dirname unless block

  turd = options['turd'] || options[:turd]

  basename = [
    Hostname,
    Ppid,
    Pid,
    Thread.current.object_id.abs,
    rand
  ].join('-')

  42.times do |n|
    pathname = File.join(dirname, "#{ basename }-n=#{ n }")

    begin
      FileUtils.mkdir_p(pathname)
    rescue Object => e
      sleep(rand)
      next
    end

    begin
      return Dir.chdir(pathname, &block)
    ensure
      FileUtils.rm_rf(pathname) unless turd
    end
  end

  raise Error, "failed to make tmpdir in #{ dirname.inspect }"
end