Class: Tmptation::TmpDir

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
InstanceTracking, SafeDeletable
Defined in:
lib/tmptation.rb

Overview

Temporary directory object which behaves like core lib’s Pathname, and allows safely deleting all of its instances.

Examples:


path = TmpDir.new
path.exist?  #=> true

TmpDir.delete_all
path.exist?  #=> false

Constant Summary

Constants included from SafeDeletable

SafeDeletable::UnsafeDelete

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InstanceTracking

included

Methods included from SafeDeletable

path_for, safe?, #safe_delete, #safe_delete_contents

Constructor Details

#initialize(prefix = 'TmpDir-') ⇒ TmpDir

Returns a new instance of TmpDir.

Parameters:

  • prefix (String) (defaults to: 'TmpDir-')

    optional prefix of directory name



198
199
200
201
# File 'lib/tmptation.rb', line 198

def initialize(prefix='TmpDir-')
  super()
  @path = Pathname(Dir.mktmpdir(prefix)).expand_path
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Delegate Pathname methods to #path

Allows TmpDir to behave like Pathname without having to use inheritence (which causes all sorts of issues).



210
211
212
213
214
215
216
217
# File 'lib/tmptation.rb', line 210

def method_missing(name, *args) #:nodoc:
  if path.respond_to?(name)
    self.class.def_delegator :@path, name #method inlining
    send(name, *args)
  else
    super
  end
end

Instance Attribute Details

#pathObject (readonly)

temporary directory’s path as a Pathname



193
194
195
# File 'lib/tmptation.rb', line 193

def path
  @path
end

Class Method Details

.delete_allObject Also known as: -@

Safe deletes and closes all instances



182
183
184
185
# File 'lib/tmptation.rb', line 182

def delete_all
  instances.each {|instance| instance.safe_delete }
  instances.clear
end