Class: Nanoc::Int::TempFilenameFactory Private

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/base/services/temp_filename_factory.rb

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTempFilenameFactory

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.

Returns a new instance of TempFilenameFactory.



14
15
16
17
# File 'lib/nanoc/base/services/temp_filename_factory.rb', line 14

def initialize
  @counts = {}
  @root_dir = Dir.mktmpdir('nanoc')
end

Instance Attribute Details

#root_dirString (readonly)

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.

Returns The root directory for all temporary filenames.

Returns:

  • (String)

    The root directory for all temporary filenames



7
8
9
# File 'lib/nanoc/base/services/temp_filename_factory.rb', line 7

def root_dir
  @root_dir
end

Class Method Details

.instanceNanoc::Int::TempFilenameFactory

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.

Returns A common instance.

Returns:



10
11
12
# File 'lib/nanoc/base/services/temp_filename_factory.rb', line 10

def self.instance
  @instance ||= new
end

Instance Method Details

#cleanup(prefix) ⇒ void

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.

This method returns an undefined value.

Parameters:

  • prefix (String)

    A string prefix that indicates which temporary filenames should be deleted.



39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ String

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.

Returns A new unused filename.

Parameters:

  • prefix (String)

    A string prefix to include in the temporary filename, often the type of filename being provided.

Returns:

  • (String)

    A new unused filename



23
24
25
26
27
28
29
30
31
32
33
# 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