123456789_123456789_123456789_123456789_123456789_

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

Class Method Summary

Class Method Details

.create(basename, tmpdir = nil, max_try: nil, **opts) (mod_func)

Generates and yields random names to create a temporary name

[ GitHub ]

  
# File 'lib/tmpdir.rb', line 140

def create(basename, tmpdir=nil, max_try: nil, **opts)
  origdir = tmpdir
  tmpdir ||= tmpdir()
  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

.tmpdir (mod_func)

[ GitHub ]

  
# File 'lib/tmpdir.rb', line 118

def tmpdir
  Dir.tmpdir
end