class Nanoc::Int::TempFilenameFactory

@api private

Attributes

root_dir[R]

@return [String] The root directory for all temporary filenames

Public Class Methods

instance() click to toggle source

@return [Nanoc::Int::TempFilenameFactory] A common instance

# File lib/nanoc/base/services/temp_filename_factory.rb, line 10
def self.instance
  @instance ||= new
end
new() click to toggle source
# File lib/nanoc/base/services/temp_filename_factory.rb, line 14
def initialize
  @counts = {}
  @root_dir = Dir.mktmpdir('nanoc')
end

Public Instance Methods

cleanup(prefix) click to toggle source

@param [String] prefix A string prefix that indicates which temporary

filenames should be deleted.

@return [void]

# File lib/nanoc/base/services/temp_filename_factory.rb, line 39
def cleanup(prefix)
  path = File.join(@root_dir, prefix)
  if File.exist?(path)
    FileUtils.rm_rf(path)
  end

  @counts.delete(prefix)

  if @counts.empty? && File.directory?(@root_dir)
    FileUtils.rm_rf(@root_dir)
  end
end
create(prefix) click to toggle source

@param [String] prefix A string prefix to include in the temporary

filename, often the type of filename being provided.

@return [String] A new unused filename

# File lib/nanoc/base/services/temp_filename_factory.rb, line 23
def create(prefix)
  count = @counts.fetch(prefix, 0)
  @counts[prefix] = count + 1

  dirname  = File.join(@root_dir, prefix)
  filename = File.join(@root_dir, prefix, count.to_s)

  FileUtils.mkdir_p(dirname)

  filename
end