Class: Temppath::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/temppath.rb

Overview

Generator generates temporary path in the base directory.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basedir, option = {}) ⇒ Generator

Returns a new instance of Generator.

Parameters:

  • basedir (Pathname)

    generator’s base directory

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

Options Hash (option):

  • :basename (String)

    prefix of filename



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/temppath.rb', line 101

def initialize(basedir, option={})
  @basedir = Pathname.new(basedir)
  @basename = (option[:basename] || "").to_s
  @unlink = true

  # extend basedir with secure methods
  @basedir.extend OriginalMethodHolder
  @basedir.extend SecurePermissionMethods

  # register a cleaner for temporary directory
  Kernel.at_exit do
    if @unlink
      remove_basedir rescue Errno::ENOENT
    end
  end
end

Instance Attribute Details

#basedirPathname (readonly)

Returns defalut base directory for paths created by Temppath.

Returns:

  • (Pathname)

    defalut base directory for paths created by Temppath



86
87
88
# File 'lib/temppath.rb', line 86

def basedir
  @basedir
end

#basenameString

Returns defalut base name.

Returns:

  • (String)

    defalut base name



90
91
92
# File 'lib/temppath.rb', line 90

def basename
  @basename
end

Returns true if unlink mode is enabled.

Returns:

  • (Boolean)

    true if unlink mode is enabled



94
95
96
# File 'lib/temppath.rb', line 94

def unlink
  @unlink
end

Instance 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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/temppath.rb', line 125

def create(option={})
  _basename = option[:basename] || @basename
  _basedir = @basedir
  if option[:basedir]
    _basedir = Pathname.new(option[:basedir])

    # extend basedir with secure methods
    _basedir.extend OriginalMethodHolder
    _basedir.extend SecurePermissionMethods
  end

  # init basedir
  unless _basedir.exist?
    _basedir.mkpath
  end

  # make a path
  path = Pathname.new(_basedir) + (_basename.to_s + generate_uuid)

  # extend path object with secure methods
  path.extend OriginalMethodHolder
  path.extend SecurePermissionMethods

  # register a file cleaner if the path is not in basedir
  if _basedir != @basedir
    Kernel.at_exit do
      if @unlink
        FileUtils.remove_entry_secure(path) rescue Errno::ENOENT
      end
    end
  end

  return path
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



169
170
171
172
173
174
# File 'lib/temppath.rb', line 169

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.



195
196
197
# File 'lib/temppath.rb', line 195

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

#touch(option = {}) ⇒ Object

Create a empty file.

Parameters:

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

Options Hash (option):

  • :mode (Integer)

    mode for the file permission

  • :basename (String)

    prefix of filename

  • :basedir (Pathname)

    pathname of base directory



185
186
187
188
189
190
# File 'lib/temppath.rb', line 185

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