Module: Pa::ClassMethods::Dir
- Included in:
- Pa
- Defined in:
- lib/tagen/core/pa/dir.rb
Instance Method Summary (collapse)
-
- (Object) each(*args, &blk)
traverse directory .
-
- (Object) each_r(*args, &blk)
each with recursive
-
each_r() skip Exception
-
each_r()err.
-
-
- (Boolean) empty?(path)
is directory empty?.
-
- (Object) glob(*args, &blk)
path globbing, exclude '.' '..' for :dotmatch.
-
- (Array<String>) ls(*args)
list directory contents.
-
- (Array<String>) ls_r(*args)
ls with recursive.
Instance Method Details
- (Enumerator<Pa>) each(path = ".", o = {}) - (nil) each(path = ".", o = {}) {|path| ... }
Note:
raise Errno::ENOTDIR, Errno::ENOENT
traverse directory
each('.', error: true).with_object([]) do |(pa,err),m|
...
end
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/tagen/core/pa/dir.rb', line 83 def each(*args, &blk) return Pa.to_enum(:each, *args) unless blk (path,), o = args. path = path ? get(path) : "." raise Errno::ENOENT, "`#{path}' doesn't exists." unless File.exists?(path) raise Errno::ENOTDIR, "`#{path}' not a directoyr." unless File.directory?(path) begin dir = Dir.open(path) rescue Errno::EPERM => err end raise err if err and !o[:error] while (entry=dir.read) next if %w(. ..).include? entry next if o[:nodot] and entry=~/^\./ next if o[:nobackup] and entry=~/~$/ # => "foo" not "./foo" pa = path=="." ? Pa(entry) : Pa(File.join(path, entry)) if o[:error] blk.call pa, err else blk.call pa end end end |
- (Enumerator<Pa>) each_r(path = ".", o = {}) - (nil) each_r(path = ".", o = {}) {|pa, relative, err| ... }
each with recursive
-
each_r() skip Exception
-
each_r()err
125 126 127 128 129 130 131 132 |
# File 'lib/tagen/core/pa/dir.rb', line 125 def each_r(*args, &blk) return Pa.to_enum(:each_r, *args) if not blk (path,), o = args. path ||= "." _each_r(path, "", o, &blk) end |
- (Boolean) empty?(path)
is directory empty?
58 |
# File 'lib/tagen/core/pa/dir.rb', line 58 def empty?(path) Dir.entries(get(path)).empty? end |
- (Array<Pa>) glob(*paths, o = {}) - (nil) glob(*paths, o = {}) {|path| ... }
Note:
glob is * ** ? [set] a,b
path globbing, exclude '.' '..' for :dotmatch
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/tagen/core/pa/dir.rb', line 30 def glob(*args, &blk) paths, o = args. paths.map!{|v|get(v)} flag = 0 o.each do |option, value| flag |= File.const_get("FNM_#{option.upcase}") if value end ret = Dir.glob(paths, flag) # delete . .. for '.*' ret.tap{|v|v.delete(*%w(. ..))} ret.map!{|v|Pa(v)} if blk ret.each {|pa| blk.call pa } else ret end end |