Class: Dir

Inherits:
Object
  • Object
show all
Defined in:
lib/mast/core_ext.rb

Overview

Metaclass extensions for core Dir class.

Class Method Summary collapse

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('**/*')


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mast/core_ext.rb', line 108

def self.multiglob(*patterns)
  options  = (Hash === patterns.last ? patterns.pop : {})

  if options.delete(:recurse)
    #patterns += patterns.collect{ |f| File.join(f, '**', '**') }
    multiglob_r(*patterns)
  end

  bitflags = 0
  bitflags |= File::FNM_NOESCAPE if options[:noescape]
  bitflags |= File::FNM_CASEFOLD if options[:casefold]
  bitflags |= File::FNM_PATHNAME if options[:pathname] or options[:strict]
  bitflags |= File::FNM_DOTMATCH if options[:dotmatch] or options[:strict]

  patterns = [patterns].flatten.compact

  if options[: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

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/**/**' )


146
147
148
149
150
151
152
153
154
155
156
# File 'lib/mast/core_ext.rb', line 146

def self.multiglob_r(*patterns)
  options = (Hash === patterns.last ? patterns.pop : {})
  matches = multiglob(*patterns)
  directories = matches.select{ |m| File.directory?(m) }
  matches += directories.collect{ |d| multiglob_r(File.join(d, '**'), options) }.flatten
  matches.uniq
  #options = (Hash === patterns.last ? patterns.pop : {})
  #options[:recurse] = true
  #patterns << options
  #multiglob(*patterns)
end