Class: TempDir

Inherits:
Object
  • Object
show all
Defined in:
lib/temp_dir.rb

Constant Summary collapse

DICT =
('a'..'z').to_a
DFLT_CREATE_ATTRS =
{:basename => "temp_dir", :rootpath => "/tmp", :name => nil}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, &block) ⇒ TempDir

Returns a new instance of TempDir.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/temp_dir.rb', line 27

def initialize(attrs, &block)
  attrs.each { |k,v|
    case k
    when :basename then @base_name = v
    when :rootpath then @root_path = v
    when :name     then @name      = v
    else
      raise "Unknown attribute #{k.inspect} => #{v.inspect}."
    end
  }
end

Class Method Details

.create(attrs = {}, &block) ⇒ Object

Usage

TempDir can be called with or without a block.

With Block

TempDir.create( [:basename => value] [, :rootpath => value] [, :name => value]) do |tmpdir|
    # Directory is automatically changed to tmpdir
    code here
end

Examples

TempDir.create { |dir|  # do work here }   # Directory is auto deleted
TempDir.create(:rootpath => "/mytmp" ) { |dir| ... }
TempDir.create(:rootpath => "./", :name => "mytempfolder") { |dir| ... }
TempDir.create(:basename => "picfiles") { |dir| ... }

Without Block

tempdir = TempDir.create
# User is responsible for removing tempdir


23
24
25
# File 'lib/temp_dir.rb', line 23

def self.create(attrs = {}, &block)
  new(DFLT_CREATE_ATTRS.merge(attrs)).send(:create, &block)
end