class MapReduce::TempPath

The MapReduce::TempPath generates a tempfile path and automatically deletes the file when the object is garbage collected or manually deleted. Using this class instead of Tempfile allows to have less open file descriptors.

Attributes

path[R]

Public Class Methods

finalize(path) click to toggle source

@api private

# File lib/map_reduce/temp_path.rb, line 27
def self.finalize(path)
  proc { FileUtils.rm_f(path) }
end
new() click to toggle source

Initializes a new tempfile path.

@example

temp_path = MapReduce::TempPath.new
File.write(temp_path.path, "blob")
# File lib/map_reduce/temp_path.rb, line 15
def initialize
  @path = Dir::Tmpname.create("") do
    # nothing
  end

  FileUtils.touch(@path)

  ObjectSpace.define_finalizer(self, self.class.finalize(@path))
end

Public Instance Methods

delete() click to toggle source

Allows to manually delete the tempfile.

@example

temp_path = MapReduce::TempPath.new
File.write(temp_path.path, "blob")
temp_path.delete
# File lib/map_reduce/temp_path.rb, line 38
def delete
  FileUtils.rm_f(path)
end