Module: Pa::ClassMethods::Dir
- Included in:
- Pa
- Defined in:
- lib/tagen/core/pa/dir.rb
Instance Method Summary collapse
-
#each(*args, &blk) ⇒ Object
traverse directory each(‘.’, error: true).with_object([]) do |(pa,err),m| …
-
#each_r(*args, &blk) ⇒ Object
each with recursive * each_r() skip Exception * each_r()err.
-
#empty?(path) ⇒ Boolean
is directory empty?.
-
#glob(*args, &blk) ⇒ Object
path globbing, exclude ‘.’ ‘..’ for :dotmatch.
-
#ls(*args) ⇒ Array<String>
list directory contents.
-
#ls_r(*args) ⇒ Array<String>
ls with recursive.
Instance Method Details
#each(path = ".", o = {}) ⇒ Enumerator<Pa> #each(path = ".", o = {}) {|path| ... } ⇒ nil
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 |
#each_r(path = ".", o = {}) ⇒ Enumerator<Pa> #each_r(path = ".", o = {}) {|pa, relative, err| ... } ⇒ nil
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 |
#empty?(path) ⇒ Boolean
is directory empty?
58 |
# File 'lib/tagen/core/pa/dir.rb', line 58 def empty?(path) Dir.entries(get(path)).empty? end |
#glob(*paths, o = {}) ⇒ Array<Pa> #glob(*paths, o = {}) {|path| ... } ⇒ nil
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 |