module BioDSL::TmpDir

Module to provide a temporary directory.

Public Class Methods

create(*files, &block) click to toggle source

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.

@param files [Array] List of file names.

@example

BioDSL::TmpDir.create do |dir|
  puts dir
    # => "<tmp_dir>"
end

@example

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