Class: Nanoc::TempFilenameFactory
- Inherits:
-
Object
- Object
- Nanoc::TempFilenameFactory
- Defined in:
- lib/nanoc/base/temp_filename_factory.rb
Instance Attribute Summary (collapse)
-
- (String) root_dir
readonly
The root directory for all temporary filenames.
Class Method Summary (collapse)
-
+ (Nanoc::TempFilenameFactory) instance
A common instance.
Instance Method Summary (collapse)
-
- (void) cleanup(prefix)
-
- (String) create(prefix)
A new unused filename.
-
- (TempFilenameFactory) initialize
constructor
A new instance of TempFilenameFactory.
Constructor Details
- (TempFilenameFactory) initialize
Returns a new instance of TempFilenameFactory
15 16 17 18 |
# File 'lib/nanoc/base/temp_filename_factory.rb', line 15 def initialize @counts = {} @root_dir = Dir.mktmpdir('nanoc') end |
Instance Attribute Details
- (String) root_dir (readonly)
Returns The root directory for all temporary filenames
8 9 10 |
# File 'lib/nanoc/base/temp_filename_factory.rb', line 8 def root_dir @root_dir end |
Class Method Details
+ (Nanoc::TempFilenameFactory) instance
Returns A common instance
11 12 13 |
# File 'lib/nanoc/base/temp_filename_factory.rb', line 11 def self.instance @instance ||= new end |
Instance Method Details
- (void) cleanup(prefix)
This method returns an undefined value.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/nanoc/base/temp_filename_factory.rb', line 40 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 |
- (String) create(prefix)
Returns A new unused filename
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/nanoc/base/temp_filename_factory.rb', line 24 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 |