Module: Pa::Directory::ClassMethods
- Defined in:
- lib/pa/directory.rb
Instance Method Summary collapse
- #each(*args, &blk) ⇒ Object
-
#each2(*args, &blk) ⇒ Object
traverse directory each(‘.’, error: true).with_object([]) do |(pa,err),m| …
-
#each2_r(*args, &blk) ⇒ Object
each with recursive * each2_r() skip Exception * each2_r()relative, err.
- #each_r(*args, &blk) ⇒ Object
-
#empty?(path) ⇒ Boolean
is directory empty?.
- #glob(*args, &blk) ⇒ Object
-
#glob2(*args, &blk) ⇒ Object
path globbing, exclude ‘.’ ‘..’.
-
#ls(*args, &blk) ⇒ Object
@return [Array<Pa>].
-
#ls2(*args, &blk) ⇒ Object
list directory contents block form is a filter.
-
#ls2_r(*args, &blk) ⇒ Object
ls2 with recursive.
- #ls_r(*args, &blk) ⇒ Object
- #tmpdir ⇒ Object
- #tmpdir2 ⇒ Object
Instance Method Details
#each(*args, &blk) ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'lib/pa/directory.rb', line 155 def each(*args, &blk) return Pa.to_enum(:each, *args) unless blk args, o = Util.(args) each2(*args, o) { |path, abs, fname, err, rea| blk.call Pa(path), abs, fname, err, rea # jruby need [] } end |
#each(path = ".", o = {}) ⇒ Enumerator<String> #each(path = ".", o = {}) {|path, abs, fname, err, rea| ... } ⇒ nil
Note:
raise Errno::ENOTDIR, Errno::ENOENT
traverse directory
each('.', error: true).with_object([]) do |(pa,err),m|
...
end
118 119 120 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 148 149 150 151 152 153 |
# File 'lib/pa/directory.rb', line 118 def each2(*args, &blk) return Pa.to_enum(:each2, *args) unless blk (dir,), o = Util.(args) dir = dir ? get(dir) : "." o = {dot: true, backup: true}.merge(o) rea_dir = o[:base_dir] ? File.join(get(o[:base_dir]), dir) : dir raise Errno::ENOENT, "`#{rea_dir}' doesn't exists." unless File.exists?(rea_dir) if not File.directory?(rea_dir) if o[:file] rea_path = rea_dir blk.call dir, File.absolute_path(rea_path, "."), File.basename(rea_path), nil, rea_path # jruby rbx return else raise Errno::ENOTDIR, "`#{rea_dir}' is not a directoy." end end begin d = Dir.open(rea_dir) rescue Errno::EPERM => err end raise err if err and !o[:error] while (entry=d.read) next if %w(. ..).include? entry next if not o[:dot] and entry=~/^\./ next if not o[:backup] and entry=~/~$/ path = Util.join_path(dir, entry) rea_path = Util.join_path(rea_dir, entry) blk.call path, File.absolute_path(rea_path, "."), File.basename(rea_path), err, rea_path # jruby rbx end end |
#each2_r(path = ".", o = {}) ⇒ Enumerator<String> #each2_r(path = ".", o = {}) {|path, abs, rel/rea, err| ... } ⇒ nil
each with recursive
-
each2_r() skip Exception
-
each2_r()relative, err
179 180 181 182 183 184 185 186 |
# File 'lib/pa/directory.rb', line 179 def each2_r(*args, &blk) return Pa.to_enum(:each2_r, *args) if not blk (dir,), o = Util.(args) dir ||= "." _each2_r(dir, "", o, &blk) end |
#each_r(*args, &blk) ⇒ Object
188 189 190 191 192 193 194 195 |
# File 'lib/pa/directory.rb', line 188 def each_r(*args, &blk) return Pa.to_enum(:each_r, *args) if not blk args, o = Util.(args) each2_r *args, o do |path, abs, rel, err, rea| blk.call Pa(path, :rel => rel), abs, rel, err, rea # jruby need [] end end |
#empty?(path) ⇒ Boolean
is directory empty?
85 86 87 |
# File 'lib/pa/directory.rb', line 85 def empty?(path) Dir.entries(get(path)).empty? end |
#glob(*args, &blk) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/pa/directory.rb', line 61 def glob(*args, &blk) args, o = Util.(args) ret = [] blk ||= proc { |path| path } glob2 *args, o do |path| ret << blk.call(Pa(path)) end ret end |
#glob2(*paths, o = {}) ⇒ Array<String> #glob2(*paths, o = {}) {|path| ... } ⇒ nil
Note:
glob is * ** ? [set] a,b
path globbing, exclude ‘.’ ‘..’
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pa/directory.rb', line 40 def glob2(*args, &blk) paths, o = Util.(args) paths.map!{|v|get(v)} blk ||= proc { |path| path } flag = 0 o.each do |option, value| flag |= File.const_get("FNM_#{option.upcase}") if value end # delete . .. files = Dir.glob(paths, flag).delete_if{|v| v =~ %r~(^|/)\.\.?$~} ret = [] files.each { |path| ret << blk.call(path) } ret end |
#ls(*paths, o = {}) ⇒ Object #ls(*paths, o = {}) {|pa, abs, fname, err, rea| ... } ⇒ Array<String>
@return [Array<Pa>]
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/pa/directory.rb', line 277 def ls(*args, &blk) dirs, o = Util.(args) blk ||= proc { true } ret = [] ls2(*dirs, o) { |path, abs, fname, err, rea| view_path = if o[:absolute] abs elsif o[:include] path else fname end ret << Pa(view_path) if blk.call(Pa(path), abs, fname, err, rea) } ret end |
#ls2(*dirs, o = {}) ⇒ Object #ls2(*dirs, o = {}) {|path, abs, fname| ... } ⇒ Array<String>
list directory contents block form is a filter.
@return [Array<String>]
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/pa/directory.rb', line 214 def ls2(*args, &blk) dirs, o = Util.(args) dirs << "." if dirs.empty? blk ||= proc { true } dirs.each.with_object([]) { |dir, m| each2(dir, o) { |path, abs, fname, err, rea| view_path = if o[:absolute] abs elsif o[:include] path else fname end m << view_path if blk.call(path, abs, fname, err, rea) } } end |
#ls2_r(*dirs, o = {}) ⇒ Array<String> #ls2_r(*dirs, o = {}) {|path, abs, rel, err, rea| ... } ⇒ Array<String>
ls2 with recursive
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/pa/directory.rb', line 247 def ls2_r(*args, &blk) dirs, o = Util.(args) dirs << "." if dirs.empty? blk ||= proc { true } dirs.each.with_object([]) { |dir, m| each2_r(dir, o) { |path, abs, rel, err, rea| view_path = if o[:absolute] abs elsif o[:include] path else rel end m << view_path if blk.call(path, abs, rel, err, rea) } } end |
#ls_r(*args, &blk) ⇒ Object
297 298 299 300 |
# File 'lib/pa/directory.rb', line 297 def ls_r(*args, &blk) args, o = Util.(args) ls2_r(*args, o, &blk) end |
#tmpdir ⇒ Object
77 78 79 |
# File 'lib/pa/directory.rb', line 77 def tmpdir Pa(Dir.tmpdir) end |
#tmpdir2 ⇒ Object
73 74 75 |
# File 'lib/pa/directory.rb', line 73 def tmpdir2 Dir.tmpdir end |