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.file? #=> true
"%o" % path.stat.mode #=> "100600"

Create a temporary directory

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

Defined Under Namespace

Modules: OriginalMethodHolder, SecurePermissionMethods

Constant Summary collapse

VERSION =

version of temppath gem

"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.basedirPathname (readonly)

Returns defalut base directory for paths created by Temppath.

Returns:

  • (Pathname)

    defalut base directory for paths created by Temppath



78
79
80
# File 'lib/temppath.rb', line 78

def basedir
  @basedir
end

Returns true if unlink mode is enabled.

Returns:

  • (Boolean)

    true if unlink mode is enabled



82
83
84
# File 'lib/temppath.rb', line 82

def unlink
  @unlink
end

Class Method Details

.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



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/temppath.rb', line 91

def create(option={})
  basename = option[:basename] || ""
  _basedir = option[:basedir] || basedir
  path = Pathname.new(_basedir) + (basename.to_s + generate_uuid)
  path.extend OriginalMethodHolder
  path.extend SecurePermissionMethods
  if _basedir != basedir
    Kernel.at_exit {FileUtils.remove_entry_secure(path) rescue Errno::ENOENT}
  end
  return path
end

.mkdir(option = {}) ⇒ Object

Create a temporary directory.



104
105
106
107
108
109
# File 'lib/temppath.rb', line 104

def mkdir(option={})
  mode = option[:mode] || 0700
  path = create(option)
  path.mkdir(mode)
  return path
end

.remove_basedirvoid

This method returns an undefined value.

Remove current temporary directory.



132
133
134
# File 'lib/temppath.rb', line 132

def remove_basedir
  FileUtils.remove_entry_secure(@basedir) if @basedir.exist?
end

.touch(option = {}) ⇒ Object

Create a empty file.



112
113
114
115
116
117
# File 'lib/temppath.rb', line 112

def touch(option={})
  mode = option[:mode] || 0600
  path = create(option)
  path.open("w", mode)
  return path
end

.update_basedirPathname

Remove current base directory, create a new base directory, and use it.

Returns:

  • (Pathname)

    new base directory



124
125
126
127
# File 'lib/temppath.rb', line 124

def update_basedir
  remove_basedir
  @basedir = create_basedir
end