Class: Nanoc::Core::TempFilenameFactory
- Inherits:
-
Object
- Object
- Nanoc::Core::TempFilenameFactory
- Defined in:
- lib/nanoc/core/temp_filename_factory.rb
Instance Attribute Summary collapse
-
#root_dir ⇒ String
readonly
The root directory for all temporary filenames.
Class Method Summary collapse
-
.instance ⇒ Nanoc::Core::TempFilenameFactory
A common instance.
Instance Method Summary collapse
- #cleanup(prefix) ⇒ void
-
#create(prefix) ⇒ String
A new unused filename.
-
#initialize ⇒ TempFilenameFactory
constructor
A new instance of TempFilenameFactory.
Constructor Details
#initialize ⇒ TempFilenameFactory
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_dir ⇒ String (readonly)
Returns 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
.instance ⇒ Nanoc::Core::TempFilenameFactory
Returns A common instance.
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.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/nanoc/core/temp_filename_factory.rb', line 43 def cleanup(prefix) path = File.join(@root_dir, prefix) FileUtils.rm_rf(path) @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.
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 |