Class: PuppetLibrary::Util::TempDir

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

Defined Under Namespace

Classes: Remover

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ TempDir

Returns a new instance of TempDir.



46
47
48
49
50
# File 'lib/puppet_library/util/temp_dir.rb', line 46

def initialize(name)
    @path = TempDir.create(name)
    @remover = Remover.new(@path)
    ObjectSpace.define_finalizer(self, @remover)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



22
23
24
# File 'lib/puppet_library/util/temp_dir.rb', line 22

def path
  @path
end

Class Method Details

.cd(name, &block) ⇒ Object



31
32
33
34
35
36
# File 'lib/puppet_library/util/temp_dir.rb', line 31

def self.cd(name, &block)
    path = create(name)
    Dir.chdir(path, &block)
ensure
    FileUtils.rm_rf path
end

.create(name) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/puppet_library/util/temp_dir.rb', line 38

def self.create(name)
    file = Tempfile.new(name)
    path = file.path
    file.unlink
    FileUtils.mkdir path
    path
end

.use(name) ⇒ Object



24
25
26
27
28
29
# File 'lib/puppet_library/util/temp_dir.rb', line 24

def self.use(name)
    path = create(name)
    yield(path)
ensure
    FileUtils.rm_rf path
end