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 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
#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.
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.
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_basedir ⇒ void
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.
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 |