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.1"

Class Method Summary collapse

Class Method Details

.basedirPathname

Return base directory of paths created by Temppath.

Returns:

  • (Pathname)

    base directory



229
230
231
# File 'lib/temppath.rb', line 229

def basedir
  @generator.basedir
end

.basenameObject

Return base name of paths created by Temppath.



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

def basename
  @generator.basename
end

.basename=(name) ⇒ Object

Set the base name.



239
240
241
# File 'lib/temppath.rb', line 239

def basename=(name)
  @generator.basename = name
end

.create(option = {}) ⇒ Object

Create a temporary path. This method creates no files.

Parameters:

  • option (Hash) (defaults to: {})

Options Hash (option):

  • :basename (String)

    prefix of filename

  • :basedir (Pathname)

    pathname of base directory



266
267
268
# File 'lib/temppath.rb', line 266

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

.create_basedirPathname

Create a new base directory.

Returns:

  • (Pathname)

    base directory



215
216
217
# File 'lib/temppath.rb', line 215

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

.mkdir(option = {}) ⇒ Object

Create a temporary directory.

Parameters:

  • option (Hash) (defaults to: {})

Options Hash (option):

  • :mode (Integer)

    mode for the directory permission

  • :basename (String)

    prefix of directory name

  • :basedir (Pathname)

    pathname of base directory



279
280
281
# File 'lib/temppath.rb', line 279

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

.remove_basedirvoid

This method returns an undefined value.

Remove current temporary directory.



304
305
306
# File 'lib/temppath.rb', line 304

def remove_basedir
  @generator.remove_basedir
end

.touch(option = {}) ⇒ Object

Create a empty file.



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

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

Return true if unlink mode is enabled.

Returns:

  • (Boolean)

    true if unlink mode is enabled



247
248
249
# File 'lib/temppath.rb', line 247

def unlink
  @generator.unlink
end

.unlink=(b) ⇒ Object

Set true or false for unlink mode.

Parameters:

  • b (Boolean)

    unlink mode



255
256
257
# File 'lib/temppath.rb', line 255

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

.update_basedir(basedir = nil) ⇒ Pathname

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

Parameters:

  • basedir (Pathname) (defaults to: nil)

    new base directory, or nil

Returns:

  • (Pathname)

    new base directory



294
295
296
297
298
299
# File 'lib/temppath.rb', line 294

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