Class: Temppath::Generator
- Inherits:
-
Object
- Object
- Temppath::Generator
- Defined in:
- lib/temppath.rb
Overview
Generator generates temporary path in the base directory.
Instance Attribute Summary collapse
-
#basedir ⇒ Pathname
readonly
Defalut base directory for paths created by Temppath.
-
#basename ⇒ String
Defalut base name.
-
#unlink ⇒ Boolean
True if unlink mode is enabled.
Instance Method Summary collapse
-
#create(option = {}) ⇒ Object
Create a temporary path.
-
#initialize(basedir, option = {}) ⇒ Generator
constructor
A new instance of Generator.
-
#mkdir(option = {}) ⇒ Object
Create a temporary directory.
-
#remove_basedir ⇒ void
Remove current temporary directory.
-
#touch(option = {}) ⇒ Object
Create a empty file.
Constructor Details
#initialize(basedir, option = {}) ⇒ Generator
Returns a new instance of Generator.
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/temppath.rb', line 101 def initialize(basedir, option={}) @basedir = Pathname.new(basedir) @basename = (option[:basename] || "").to_s @unlink = true # register a cleaner for temporary directory Kernel.at_exit do if @unlink remove_basedir rescue Errno::ENOENT end end end |
Instance Attribute Details
#basedir ⇒ Pathname (readonly)
Returns defalut base directory for paths created by Temppath.
86 87 88 |
# File 'lib/temppath.rb', line 86 def basedir @basedir end |
#basename ⇒ String
Returns defalut base name.
90 91 92 |
# File 'lib/temppath.rb', line 90 def basename @basename end |
#unlink ⇒ Boolean
Returns 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.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/temppath.rb', line 121 def create(option={}) _basename = option[:basename] || @basename _basedir = Pathname.new(option[:basedir] || @basedir) # init basedir unless _basedir.exist? _basedir.mkdir(0700) 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.
158 159 160 161 162 163 |
# File 'lib/temppath.rb', line 158 def mkdir(option={}) mode = option[:mode] || 0700 path = create(option) path.mkdir(mode) return path end |
#remove_basedir ⇒ void
This method returns an undefined value.
Remove current temporary directory.
184 185 186 |
# File 'lib/temppath.rb', line 184 def remove_basedir FileUtils.remove_entry_secure(@basedir) if @basedir.exist? end |
#touch(option = {}) ⇒ Object
Create a empty file.
174 175 176 177 178 179 |
# File 'lib/temppath.rb', line 174 def touch(option={}) mode = option[:mode] || 0600 path = create(option) path.open("w", mode) return path end |