Module: Temppath

Defined in:
lib/temppath.rb,
lib/temppath/version.rb

Overview

Temppath creates temporary path. The differences from standard tempfile.rb are that this library generates Pathname objects with no files and filenames are based on UUID. Files in paths generated by this are deleted when Ruby exits.

Examples:

Create a temporary path

path = Temppath.create
#=> #<Pathname:/tmp/ruby-temppath-20130407-5775-w5k77l/f41bd6c5-fc99-4b7a-8f68-95b7ae4a6b22>
path.exist? #=> false
path.open("w")
"%o" % path.stat.mode #=> "100600" (default permission 0600)

Touch a temporary file

path = Temppath.touch
path.exist? #=> true
path.file?  #=> true
"%o" % path.stat.mode #=> "100600"

Create a temporary directory

path = Temppath.mkdir
path.exist?     #=> true
path.directory? #=> true
"%o" % path.stat.mode #=> "40700"

Use temporary path generator

temppath = Temppath::Generator.new("/tmp/other-dir")
temppath.create
temppath.mkdir
temppath.touch

Defined Under Namespace

Modules: OriginalMethodHolder, SecurePermissionMethods Classes: Generator

Constant Summary collapse

VERSION =

version of temppath gem

"0.3.0"

Class Method Summary collapse

Class Method Details

.basedirPathname

Return base directory of paths created by Temppath.



218
219
220
# File 'lib/temppath.rb', line 218

def basedir
  @generator.basedir
end

.create(option = {}) ⇒ Object

Create a temporary path. This method creates no files.

Options Hash (option):

  • :basename (String)

    prefix of filename

  • :basedir (Pathname)

    pathname of base directory



245
246
247
# File 'lib/temppath.rb', line 245

def create(option={})
  @generator.create(option)
end

.create_basedirPathname

Create a new base directory.



204
205
206
# File 'lib/temppath.rb', line 204

def create_basedir
  Pathname.new(Dir.mktmpdir("ruby-temppath-"))
end

.mkdir(option = {}) ⇒ Object

Create a temporary directory.

Options Hash (option):

  • :mode (Integer)

    mode for the directory permission

  • :basename (String)

    prefix of directory name

  • :basedir (Pathname)

    pathname of base directory



258
259
260
# File 'lib/temppath.rb', line 258

def mkdir(option={})
  @generator.mkdir(option)
end

.remove_basedirvoid

This method returns an undefined value.

Remove current temporary directory.



283
284
285
# File 'lib/temppath.rb', line 283

def remove_basedir
  @generator.remove_basedir
end

.touch(option = {}) ⇒ Object

Create a empty file.



263
264
265
# File 'lib/temppath.rb', line 263

def touch(option={})
  @generator.touch(option)
end

Return true if unlink mode is enabled.



226
227
228
# File 'lib/temppath.rb', line 226

def unlink
  @generator.unlink
end

.unlink=(b) ⇒ Object

Set true or false unlink mode.



234
235
236
# File 'lib/temppath.rb', line 234

def unlink=(b)
  @generator.unlink = b
end

.update_basedir(basedir = nil) ⇒ Pathname

Remove current base directory and change to use a new base directory.



273
274
275
276
277
278
# File 'lib/temppath.rb', line 273

def update_basedir(basedir=nil)
  @generator.remove_basedir
  _basedir = basedir || create_basedir
  @generator = Generator.new(_basedir)
  return _basedir
end