Class: Nanoc::Core::TempFilenameFactory

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTempFilenameFactory

Returns a new instance of TempFilenameFactory.



14
15
16
17
18
# File 'lib/nanoc/core/temp_filename_factory.rb', line 14

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

Instance Attribute Details

#root_dirString (readonly)

Returns The root directory for all temporary filenames.

Returns:

  • (String)

    The root directory for all temporary filenames



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

def root_dir
  @root_dir
end

Class Method Details

.instanceNanoc::Core::TempFilenameFactory

Returns A common instance.

Returns:



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

def self.instance
  @instance ||= new
end

Instance Method Details

#cleanup(prefix) ⇒ void

This method returns an undefined value.

Parameters:

  • prefix (String)

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



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nanoc/core/temp_filename_factory.rb', line 43

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

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nanoc/core/temp_filename_factory.rb', line 24

def create(prefix)
  count = nil
  @mutex.synchronize do
    count = @counts.fetch(prefix, 0)
    @counts[prefix] = count + 1
  end

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

  FileUtils.mkdir_p(dirname)

  filename
end