Class: Bio::Command::Tmpdir

Inherits:
Object show all
Defined in:
lib/bio/command.rb

Overview

Bio::Command::Tmpdir is a wrapper class to handle temporary directory like Tempfile class. A temporary directory is created when the object of the class is created, and automatically removed when the object is destroyed by GC.

BioRuby library internal use only.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix_suffix = nil, tmpdir = nil) ⇒ Tmpdir

Creates a new Tmpdir object. The arguments are the same as Bio::Command.mktmpdir.


Arguments:

  • (optional) prefix_suffix: String (or Array)

  • (optional) tmpdir: String: temporary directory's path

Returns

Tmpdir object



586
587
588
589
590
591
# File 'lib/bio/command.rb', line 586

def initialize(prefix_suffix = nil, tmpdir = nil)
  @data = []
  @clean_proc = self.class.callback(@data)
  ObjectSpace.define_finalizer(self, @clean_proc)
  @data.push(@path = Bio::Command.mktmpdir(prefix_suffix, tmpdir).freeze)
end

Class Method Details

.callback(data) ⇒ Object

Returns finalizer object for Tmpdir class. Internal use only. Users should not call this method directly.

Acknowledgement: The essense of the code is taken from tempfile.rb in Ruby 1.8.7.


Arguments:

  • (required) data: Array containing internal data

Returns

Proc object



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/bio/command.rb', line 560

def self.callback(data)
  pid = $$
  lambda {
    path, = *data
    if pid == $$
      $stderr.print "removing ", path, " ..." if $DEBUG
      if path and !path.empty? and
          File.directory?(path) and
          !File.symlink?(path) then
        Bio::Command.remove_entry_secure(path)
        $stderr.print "done\n" if $DEBUG
      else
        $stderr.print "skipped\n" if $DEBUG
      end
    end
  }
end

Instance Method Details

#close!Object

Removes the temporary directory.

Returns

nil



603
604
605
606
607
608
609
610
611
612
# File 'lib/bio/command.rb', line 603

def close!
  # raise error if path is nil
  self.path
  # finilizer object is called to remove the directory
  @clean_proc.call
  # unregister finalizer
  ObjectSpace.undefine_finalizer(self)
  # @data and @path is removed
  @data = @path = nil
end

#pathObject

Path to the temporay directory

Returns

String



596
597
598
# File 'lib/bio/command.rb', line 596

def path
  @path || raise(IOError, 'removed temporary directory')
end