Class: Tmptation::TmpFile

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

Overview

Subclass of core lib’s Tempfile that allows safely deleting all of its instances. It also provides a convenient way to add content to the file.

Examples:


file = TmpFile.new('name', 'contents')

file.path.class   #=> Pathname
file.path.exist?  #=> true
file.closed?      #=> false
file.read         #=> "contents"

TmpFile.delete_all

file.path.exist?  #=> false
file.closed?      #=> true

Constant Summary

Constants included from SafeDeletable

SafeDeletable::UnsafeDelete

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(name = 'anon', body = '') ⇒ TmpFile

Returns a new instance of TmpFile.

Parameters:

  • name (String) (defaults to: 'anon')

    optional prefix name of file

  • body (String) (defaults to: '')

    optional contents of file



148
149
150
151
152
# File 'lib/tmptation.rb', line 148

def initialize(name='anon', body='')
  super(name)
  self << body
  self.rewind
end

Class Method Details

.delete_allObject Also known as: -@

Safe deletes and closes all instances



132
133
134
135
136
137
138
# File 'lib/tmptation.rb', line 132

def delete_all
  instances.each do |instance|
    instance.safe_delete
    instance.close
  end
  instances.clear
end

Instance Method Details

#pathObject

File’s path as a Pathname



158
159
160
# File 'lib/tmptation.rb', line 158

def path
  Pathname(super)
end