Ratch  Ratch::FileList

[Validate]
Generated with Newfish 0.1.0

FileList

A FileList is essentially an array with a few helper methods defined to make file manipulation a bit easier.

FileLists are lazy. When given a list of glob patterns for possible files to be included in the file list, instead of searching the file structures to find the files, a FileList holds the pattern for latter use.

This allows us to define a number of FileList to match any number of files, but only search out the actual files when then FileList itself is actually used. The key is that the first time an element of the FileList/Array is requested, the pending patterns are resolved into a real list of file names.

Constants

ARRAY_METHODS

List of array methods (that are not in Object) that need to be delegated.

MUST_DEFINE

List of additional methods that must be delegated.

MUST_NOT_DEFINE

List of methods that should not be delegated here (we define special versions of them explicitly below).

SPECIAL_RETURN

List of delegated methods that return new array values which need wrapping.

DELEGATING_METHODS
DEFAULT_IGNORE_PATTERNS
DEFAULT_IGNORE_PROCS

Public Class Methods

[](*args) click to toggle source

Create a new file list including the files listed. Similar to:

  FileList.new(*args)
    # File lib/ratch/file_list.rb, line 83
83:     def self.[](*args)
84:       new(*args)
85:     end
all(*args) click to toggle source

Same a new but removes the default exclusions.

    # File lib/ratch/file_list.rb, line 88
88:     def self.all(*args)
89:       obj = new(*args)
90:       obj.clear_exclude
91:       obj
92:     end
new(*patterns) click to toggle source

Create a file list from the globbable patterns given. If you wish to perform multiple includes or excludes at object build time, use the “yield self” pattern.

Example:

  file_list = FileList.new('lib/**/*.rb', 'test/test*.rb')

  pkg_files = FileList.new('lib/**/*') do |fl|
    fl.exclude(/\bCVS\b/)
  end
     # File lib/ratch/file_list.rb, line 105
105:     def initialize(*patterns)
106:       @pending_add = []
107:       @pending = false
108:       @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
109:       @exclude_procs    = DEFAULT_IGNORE_PROCS.dup
110:       @items = []
111:       patterns.each { |pattern| include(pattern) }
112:       yield self if block_given?
113:     end

Public Instance Methods

*(other) click to toggle source

Redefine * to return either a string or a new file list.

     # File lib/ratch/file_list.rb, line 198
198:     def *(other)
199:       result = @items * other
200:       case result
201:       when Array
202:         FileList.new.import(result)
203:       else
204:         result
205:       end
206:     end
==(array) click to toggle source

Define equality.

     # File lib/ratch/file_list.rb, line 176
176:     def ==(array)
177:       to_ary == array
178:     end
add(*filenames) click to toggle source
clear_exclude() click to toggle source

Clear all the exclude patterns so that we exclude nothing.

     # File lib/ratch/file_list.rb, line 169
169:     def clear_exclude
170:       @exclude_patterns = []
171:       @exclude_procs    = []
172:       self
173:     end
clone() click to toggle source
     # File lib/ratch/file_list.rb, line 390
390:     def clone
391:       sibling = dup
392:       sibling.freeze if frozen?
393:       sibling
394:     end
dup() click to toggle source

Clone an object by making a new object and setting all the instance variables to the same values.

     # File lib/ratch/file_list.rb, line 379
379:     def dup
380:       sibling = self.class.new
381:       instance_variables.each do |ivar|
382:         value = self.instance_variable_get(ivar)
383:         new_value = value.clone rescue value
384:         sibling.instance_variable_set(ivar, new_value)
385:       end
386:       sibling.taint if tainted?
387:       sibling
388:     end
egrep(pattern, *options) click to toggle source

Grep each of the files in the filelist using the given pattern. If a block is given, call the block on each matching line, passing the file name, line number, and the matching line of text. If no block is given, a standard emac style file:linenumber:line message will be printed to standard out. Returns the number of matched items.

     # File lib/ratch/file_list.rb, line 293
293:     def egrep(pattern, *options)
294:       matched = 0
295:       each do |fn|
296:         begin
297:           open(fn, "rb", *options) do |inf|
298:             count = 0
299:             inf.each do |line|
300:               count += 1
301:               if pattern.match(line)
302:                 matched += 1
303:                 if block_given?
304:                   yield fn, count, line
305:                 else
306:                   puts "#{fn}:#{count}:#{line}"
307:                 end
308:               end
309:             end
310:           end
311:         rescue StandardError => ex
312:           puts "Error while processing '#{fn}': #{ex}"
313:         end
314:       end
315:       matched
316:     end
exclude(*patterns, &block) click to toggle source

Register a list of file name patterns that should be excluded from the list. Patterns may be regular expressions, glob patterns or regular strings. In addition, a block given to exclude will remove entries that return true when given to the block.

Note that glob patterns are expanded against the file system. If a file is explicitly added to a file list, but does not exist in the file system, then an glob pattern in the exclude list will not exclude the file.

Examples:

  FileList['a.c', 'b.c'].exclude("a.c") => ['b.c']
  FileList['a.c', 'b.c'].exclude(/^a/)  => ['b.c']

If “a.c“ is a file, then …

  FileList['a.c', 'b.c'].exclude("a.*") => ['b.c']

If “a.c“ is not a file, then …

  FileList['a.c', 'b.c'].exclude("a.*") => ['a.c', 'b.c']
     # File lib/ratch/file_list.rb, line 156
156:     def exclude(*patterns, &block)
157:       patterns.each do |pat|
158:         @exclude_patterns << pat
159:       end
160:       if block_given?
161:         @exclude_procs << block
162:       end
163:       resolve_exclude if ! @pending
164:       self
165:     end
exclude?(fn) click to toggle source

Should the given file name be excluded?

     # File lib/ratch/file_list.rb, line 358
358:     def exclude?(fn)
359:       return true if @exclude_patterns.any? do |pat|
360:         case pat
361:         when Regexp
362:           fn =~ pat
363:         when /[*?]/
364:           File.fnmatch?(pat, fn, File::FNM_PATHNAME)
365:         else
366:           fn == pat
367:         end
368:       end
369:       @exclude_procs.any? { |p| p.call(fn) }
370:     end
existing() click to toggle source

Return a new file list that only contains file names from the current file list that exist on the file system.

     # File lib/ratch/file_list.rb, line 320
320:     def existing
321:       select { |fn| File.exist?(fn) }
322:     end
existing!() click to toggle source

Modify the current file list so that it contains only file name that exist on the file system.

     # File lib/ratch/file_list.rb, line 326
326:     def existing!
327:       resolve
328:       @items = @items.select { |fn| File.exist?(fn) }
329:       self
330:     end
ext(newext='') click to toggle source

Return a new FileList with String#ext method applied to each member of the array.

This method is a shortcut for:

   array.collect { |item| item.ext(newext) }

ext is a user added method for the Array class.

     # File lib/ratch/file_list.rb, line 283
283:     def ext(newext='')
284:       collect { |fn| fn.ext(newext) }
285:     end
gsub(pat, rep) click to toggle source

Return a new FileList with the results of running gsub against each element of the original list.

Example:

  FileList['lib/test/file', 'x/y'].gsub(/\//, "\\")
     => ['lib\\test\\file', 'x\\y']
     # File lib/ratch/file_list.rb, line 252
252:     def gsub(pat, rep)
253:       inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
254:     end
gsub!(pat, rep) click to toggle source

Same as gsub except that the original file list is modified.

     # File lib/ratch/file_list.rb, line 263
263:     def gsub!(pat, rep)
264:       each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
265:       self
266:     end
import(array) click to toggle source
     # File lib/ratch/file_list.rb, line 372
372:     def import(array)
373:       @items = array
374:       self
375:     end
include(*filenames) click to toggle source

Add file names defined by glob patterns to the file list. If an array is given, add each element of the array.

Example:

  file_list.include("*.java", "*.cfg")
  file_list.include %w( math.c lib.h *.o )
     # File lib/ratch/file_list.rb, line 122
122:     def include(*filenames)
123:       # TODO: check for pending
124:       filenames.each do |fn|
125:         if fn.respond_to? :to_ary
126:           include(*fn.to_ary)
127:         else
128:           @pending_add << fn
129:         end
130:       end
131:       @pending = true
132:       self
133:     end
Also aliased as: add
is_a?(klass) click to toggle source

Lie about our class.

     # File lib/ratch/file_list.rb, line 192
192:     def is_a?(klass)
193:       klass == Array || super(klass)
194:     end
Also aliased as: kind_of?
kind_of?(klass) click to toggle source
pathmap(spec=nil) click to toggle source

Apply the pathmap spec to each of the included file names, returning a new file list with the modified paths. (See String#pathmap for details.)

     # File lib/ratch/file_list.rb, line 271
271:     def pathmap(spec=nil)
272:       collect { |fn| fn.pathmap(spec) }
273:     end
resolve() click to toggle source

Resolve all the pending adds now.

     # File lib/ratch/file_list.rb, line 209
209:     def resolve
210:       if @pending
211:         @pending = false
212:         @pending_add.each do |fn| resolve_add(fn) end
213:         @pending_add = []
214:         resolve_exclude
215:       end
216:       self
217:     end
sub(pat, rep) click to toggle source

Return a new FileList with the results of running sub against each element of the orignal list.

Example:

  FileList['a.c', 'b.c'].sub(/\.c$/, '.o')  => ['a.o', 'b.o']
     # File lib/ratch/file_list.rb, line 241
241:     def sub(pat, rep)
242:       inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) }
243:     end
sub!(pat, rep) click to toggle source

Same as sub except that the oringal file list is modified.

     # File lib/ratch/file_list.rb, line 257
257:     def sub!(pat, rep)
258:       each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
259:       self
260:     end
to_a() click to toggle source

Return the internal array object.

     # File lib/ratch/file_list.rb, line 181
181:     def to_a
182:       resolve
183:       @items
184:     end
to_ary() click to toggle source

Return the internal array object.

     # File lib/ratch/file_list.rb, line 187
187:     def to_ary
188:       to_a
189:     end
to_s() click to toggle source

Convert a FileList to a string by joining all elements with a space.

     # File lib/ratch/file_list.rb, line 344
344:     def to_s
345:       resolve
346:       self.join(' ')
347:     end

Private Instance Methods

add_matching(pattern) click to toggle source

Add matching glob patterns.

     # File lib/ratch/file_list.rb, line 350
350:     def add_matching(pattern)
351:       Dir[pattern].each do |fn|
352:         self << fn unless exclude?(fn)
353:       end
354:     end
resolve_add(fn) click to toggle source
     # File lib/ratch/file_list.rb, line 219
219:     def resolve_add(fn)
220:       case fn
221:       when %{[*?\[\{]}
222:         add_matching(fn)
223:       else
224:         self << fn
225:       end
226:     end
resolve_exclude() click to toggle source
     # File lib/ratch/file_list.rb, line 229
229:     def resolve_exclude
230:       reject! { |fn| exclude?(fn) }
231:       self
232:     end

Disabled; run with --debug to generate this.