Module: BioDSL::TmpDir

Defined in:
lib/BioDSL/tmp_dir.rb

Overview

Module to provide a temporary directory.

Class Method Summary collapse

Class Method Details

.create(*files, &block) ⇒ Object

Create a temporary directory in block context. The directory is deleted when the TmpDir object is garbage collected or the Ruby intepreter exits. If called with a list of filenames, these are provided as block arguments such that the files parent are the temporary directory. However, the last block argument is always the path to the temporary directory.

Examples:

BioDSL::TmpDir.create do |dir|
  puts dir
    # => "<tmp_dir>"
end
BioDSL::TmpDir.create("foo", "bar") do |foo, bar, dir|
  puts foo
    # => "<tmp_dir>/foo"
  puts bar
    # => "<tmp_dir>/foo"
  puts dir
    # => "<tmp_dir>"
end

Parameters:

  • files (Array)

    List of file names.


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/BioDSL/tmp_dir.rb', line 54

def self.create(*files, &block)
  fail 'no block given' unless block

  Dir.mktmpdir(nil, BioDSL::Config::TMP_DIR) do |dir|
    paths = files.each_with_object([]) { |e, a| a << File.join(dir, e) }

    if paths.empty?
      block.call(dir)
    else
      block.call(paths << dir)
    end
  end
end