Class: Dir
- Inherits:
-
Object
- Object
- Dir
- Defined in:
- lib/folio/multiglob.rb
Class Method Summary collapse
-
.multiglob(*patterns) ⇒ Object
Like
globbut can take multiple patterns. -
.multiglob_r(*patterns) ⇒ Object
The same as
multiglob, but recusively includes directories.
Class Method Details
.multiglob(*patterns) ⇒ Object
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('**/*')
– DEPRECATED
Multiglob also accepts ‘+’ and ‘-’ prefixes. Any entry that begins with a ‘-’ is treated as an exclusion glob and will be removed from the final result. For example, to collect all files in the current directory, less ruby scripts:
Dir.multiglob( '*', '-*.rb' )
This is very useful in collecting files as specificed by a configuration parameter. ++
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/folio/multiglob.rb', line 40 def self.multiglob(*patterns) = (Hash === patterns.last ? patterns.pop : {}) if .delete(:recurse) #patterns += patterns.collect{ |f| File.join(f, '**', '**') } multiglob_r(*patterns) end bitflags = 0 bitflags |= File::FNM_NOESCAPE if [:noescape] bitflags |= File::FNM_CASEFOLD if [:casefold] bitflags |= File::FNM_PATHNAME if [:pathname] or [:strict] bitflags |= File::FNM_DOTMATCH if [:dotmatch] or [:strict] patterns = [patterns].flatten.compact if [:recurse] patterns += patterns.collect{ |f| File.join(f, '**', '**') } end files = [] files += patterns.collect{ |pattern| Dir.glob(pattern, bitflags) }.flatten.uniq return files end |
.multiglob_r(*patterns) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/folio/multiglob.rb', line 78 def self.multiglob_r(*patterns) = (Hash === patterns.last ? patterns.pop : {}) matches = multiglob(*patterns) directories = matches.select{ |m| File.directory?(m) } matches += directories.collect{ |d| multiglob_r(File.join(d, '**'), ) }.flatten matches.uniq #options = (Hash === patterns.last ? patterns.pop : {}) #options[:recurse] = true #patterns << options #multiglob(*patterns) end |