Class: MapReduce::TempPath

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

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTempPath

Initializes a new tempfile path.

Examples:

temp_path = MapReduce::TempPath.new
File.write(temp_path.path, "blob")


15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/map_reduce/temp_path.rb', line 7

def path
  @path
end

Class Method Details

.finalize(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
# File 'lib/map_reduce/temp_path.rb', line 27

def self.finalize(path)
  proc { FileUtils.rm_f(path) }
end

Instance Method Details

#deleteObject

Allows to manually delete the tempfile.

Examples:

temp_path = MapReduce::TempPath.new
File.write(temp_path.path, "blob")
temp_path.delete


38
39
40
# File 'lib/map_reduce/temp_path.rb', line 38

def delete
  FileUtils.rm_f(path)
end