Module: Dir::Tmpname
Do not use. This module is for internal use only.
Relationships & Source Files | |
Defined in: | lib/tmpdir.rb |
Overview
Temporary name generator
Constant Summary
-
RANDOM =
private
Dedicated random number generator
Object.new
-
UNUSABLE_CHARS =
Unusable characters as path name
"^,-.0-9A-Z_a-z~"
Class Method Summary
-
.create(basename, tmpdir = nil, max_try: nil, **opts)
mod_func
Generates and yields random names to create a temporary name.
- .tmpdir mod_func
Class Method Details
.create(basename, tmpdir = nil, max_try: nil, **opts) (mod_func)
Generates and yields random names to create a temporary name
# File 'lib/tmpdir.rb', line 151
def create(basename, tmpdir=nil, max_try: nil, **opts) origdir = tmpdir if tmpdir raise ArgumentError, "empty parent path" if tmpdir.empty? else tmpdir = tmpdir() end n = nil prefix, suffix = basename prefix = (String.try_convert(prefix) or raise ArgumentError, "unexpected prefix: #{prefix.inspect}") prefix = prefix.delete(UNUSABLE_CHARS) suffix &&= (String.try_convert(suffix) or raise ArgumentError, "unexpected suffix: #{suffix.inspect}") suffix &&= suffix.delete(UNUSABLE_CHARS) begin t = Time.now.strftime("%Y%m%d") path = "#{prefix}#{t}-#{$$}-#{RANDOM.next}"\ "#{n ? %[-#{n}] : ''}#{suffix||''}" path = File.join(tmpdir, path) yield(path, n, opts, origdir) rescue Errno::EEXIST n ||= 0 n += 1 retry if !max_try or n < max_try raise "cannot generate temporary name using '#{basename}' under '#{tmpdir}'" end path end