Module: FakeFS::Dir::Tmpname

Defined in:
lib/fakefs/dir.rb

Overview

Tmpname module

Class Method Summary collapse

Class Method Details

.create(basename, *rest) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/fakefs/dir.rb', line 210

def create(basename, *rest)
  if (opts = Hash.try_convert(rest[-1]))
    opts = opts.dup if rest.pop.equal?(opts)
    max_try = opts.delete(:max_try)
  else
    opts = {}
  end
  tmpdir, = *rest
  tmpdir ||= self.tmpdir
  Dir.mkdir(tmpdir) unless Dir.exist?(tmpdir)

  n = nil
  begin
    path = File.join(tmpdir, make_tmpname(basename, n))
    yield(path, n, opts)
  rescue Errno::EEXIST
    n ||= 0
    n += 1
    retry if !max_try || n < max_try
    raise "cannot generate temporary name using `#{basename}' " \
      "under `#{tmpdir}'"
  end
  path
end

.make_tmpname(prefix_suffix, suffix) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/fakefs/dir.rb', line 193

def make_tmpname(prefix_suffix, suffix)
  case prefix_suffix
  when String
    prefix = prefix_suffix
    suffix = ''
  when Array
    prefix = prefix_suffix[0]
    suffix = prefix_suffix[1]
  else
    raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
  end
  t = Time.now.strftime('%Y%m%d')
  path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
  path << "-#{suffix}" if suffix
  path << suffix
end

.tmpdirObject



189
190
191
# File 'lib/fakefs/dir.rb', line 189

def tmpdir
  Dir.tmpdir
end