| Class | Dir |
| In: |
lib/core/facets/dir/multiglob.rb
lib/core/facets/dir/ascend.rb lib/core/facets/dir/recurse.rb lib/core/facets/dir/parent.rb |
| Parent: | Object |
Ascend a directory path.
Dir.ascend("/var/log") do |path|
p path
end
produces
/var/log /var /
CREDIT: Daniel Berger, Jeffrey Schwab
# File lib/core/facets/dir/ascend.rb, line 19
19: def self.ascend(dir, inclusive=true, &blk)
20: dir = dir.dup
21: blk.call(dir) if inclusive
22: ri = dir.rindex('/')
23: while ri
24: dir = dir.slice(0...ri)
25: if dir == ""
26: blk.call('/') ; break
27: end
28: blk.call( dir )
29: ri = dir.rindex('/')
30: end
31: end
Descend a directory path.
Dir.descend("/var/log") do |path|
p path
end
produces
/ /var /var/log
CREDIT: Daniel Berger, Jeffrey Schwab
# File lib/core/facets/dir/ascend.rb, line 47
47: def self.descend(path) #:yield:
48: paths = path.split('/')
49: paths.size.times do |n|
50: pth = File.join(*paths[0..n])
51: pth = "/" if pth == ""
52: yield(pth)
53: end
54: end
Same as Dir#recurse.
# File lib/core/facets/dir/recurse.rb, line 23
23: def self.ls_r(path='.', &block)
24: recurse(path, &block)
25: end
Like glob but can take multiple patterns.
Dir.multiglob( '*.rb', '*.py' )
Rather then constants for options multiglob accepts a trailing options hash of symbol keys.
:noescape File::FNM_NOESCAPE :casefold File::FNM_CASEFOLD :pathname File::FNM_PATHNAME :dotmatch File::FNM_DOTMATCH :strict File::FNM_PATHNAME && File::FNM_DOTMATCH
It also has an option for recurse.
:recurse Recurively include contents of directories.
For example
Dir.multiglob( '*', :recurse => true )
would have the same result as
Dir.multiglob('**/*')
# File lib/core/facets/dir/multiglob.rb, line 40
40: def self.multiglob(*patterns)
41: options = (Hash === patterns.last ? patterns.pop : {})
42:
43: if options.delete(:recurse)
44: #patterns += patterns.collect{ |f| File.join(f, '**', '**') }
45: multiglob_r(*patterns)
46: end
47:
48: bitflags = 0
49: bitflags |= File::FNM_NOESCAPE if options[:noescape]
50: bitflags |= File::FNM_CASEFOLD if options[:casefold]
51: bitflags |= File::FNM_PATHNAME if options[:pathname] or options[:strict]
52: bitflags |= File::FNM_DOTMATCH if options[:dotmatch] or options[:strict]
53:
54: patterns = [patterns].flatten.compact
55:
56: if options[:recurse]
57: patterns += patterns.collect{ |f| File.join(f, '**', '**') }
58: end
59:
60: files = []
61: files += patterns.collect{ |pattern| Dir.glob(pattern, bitflags) }.flatten.uniq
62:
63: return files
64: end
The same as multiglob, but recusively includes directories.
Dir.multiglob_r( 'folder' )
is equivalent to
Dir.multiglob( 'folder', :recurse=>true )
The effect of which is
Dir.multiglob( 'folder', 'folder/**/**' )
# File lib/core/facets/dir/multiglob.rb, line 78
78: def self.multiglob_r(*patterns)
79: options = (Hash === patterns.last ? patterns.pop : {})
80: matches = multiglob(*patterns)
81: directories = matches.select{ |m| File.directory?(m) }
82: matches += directories.collect{ |d| multiglob_r(File.join(d, '**'), options) }.flatten
83: matches.uniq
84: #options = (Hash === patterns.last ? patterns.pop : {})
85: #options[:recurse] = true
86: #patterns << options
87: #multiglob(*patterns)
88: end
Is a path parental to another?
TODO: Needs improvement. TODO: Instance version?
# File lib/core/facets/dir/parent.rb, line 8
8: def self.parent?(parent_path, child_path)
9: %r|^#{Regexp.escape(parent_path)}| =~ child_path
10: end
Recursively scan a directory and pass each file to the given block.
CREDIT: George Moschovitis
# File lib/core/facets/dir/recurse.rb, line 7
7: def self.recurse(path='.', &block)
8: list = []
9: stoplist = ['.', '..']
10: Dir.foreach(path) do |f|
11: next if stoplist.include?(f)
12: filename = (path == '.' ? f : path + '/' + f)
13: list << filename
14: block.call(filename) if block
15: if FileTest.directory?(filename) and not FileTest.symlink?(filename)
16: list.concat( Dir.recurse(filename, &block) )
17: end
18: end
19: list
20: end