Class PathList
In: lib/path_list.rb
Parent: Object

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

PathLists 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 PathList holds the pattern for latter use.

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

Methods

Classes and Modules

Class PathList::PathAndMatchData

Constants

ARRAY_METHODS = Array.instance_methods - Object.instance_methods unless defined? ARRAY_METHODS   List of array methods (that are not in Object) that need to be delegated.
MUST_DEFINE = %w[to_a inspect] unless defined? MUST_DEFINE   List of additional methods that must be delegated.
MUST_NOT_DEFINE = %w[to_a to_ary partition *] unless defined? MUST_NOT_DEFINE   List of methods that should not be delegated here (we define special versions of them explicitly below).
SPECIAL_RETURN = %w[ map collect sort sort_by select find_all reject grep compact flatten uniq values_at + - & | ] unless defined? SPECIAL_RETURN   List of delegated methods that return new array values which need wrapping.
DELEGATING_METHODS = (ARRAY_METHODS + MUST_DEFINE - MUST_NOT_DEFINE).sort.uniq \ unless defined? DELEGATING_METHODS
DEFAULT_IGNORE_PATTERNS = [ /(^|[\/\\])CVS([\/\\]|$)/, /(^|[\/\\])\.svn([\/\\]|$)/, /\.bak$/, /~$/, /(^|[\/\\])core$/

External Aliases

each -> traditional_each
  <<<<

Public Class methods

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

  PathList.new(*args)

[Source]

# File lib/path_list.rb, line 440
    def [](*args)
      new(*args)
    end

Clear the ignore patterns.

[Source]

# File lib/path_list.rb, line 464
    def clear_ignore_patterns
      @exclude_patterns = [ /^$/ ]
    end

[Source]

# File lib/path_list.rb, line 444
    def import(array)
      new.import(array)
    end

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 = PathList.new['lib/**/*.rb', 'test/test*.rb']

  pkg_files = PathList.new['lib/**/*'] do |fl|
    fl.exclude(/\bCVS\b/)
  end

[Source]

# File lib/path_list.rb, line 136
  def initialize(*patterns)
    @pending_add = []
    @pending = false
    @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
    @exclude_re = nil
    @items = []
    patterns.each { |pattern| include(pattern) }
    yield self if block_given?
  end

Set the ignore patterns back to the default value. The default patterns will ignore files

  • containing "CVS" in the file path
  • containing ".svn" in the file path
  • ending with ".bak"
  • ending with "~"
  • named "core"

Note that file names beginning with "." are automatically ignored by Ruby‘s glob patterns and are not specifically listed in the ignore patterns.

[Source]

# File lib/path_list.rb, line 459
    def select_default_ignore_patterns
      @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
    end

[Source]

# File lib/path_list.rb, line 362
  def self.yaml_load ( val )
    new(val)
  end

Public Instance methods

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

[Source]

# File lib/path_list.rb, line 222
  def *(other)
    result = @items * other
    case result
    when Array
      PathList.import result
    else
      result
    end
  end

Define equality.

[Source]

# File lib/path_list.rb, line 205
  def ==(array)
    to_ary == array
  end
add(*filenames)

Alias for include

[Source]

# File lib/path_list.rb, line 243
  def calculate_exclude_regexp
    ignores = []
    @exclude_patterns.each do |pat|
      case pat
      when Regexp
        ignores << pat
      when /[*.]/
        Dir[pat].each do |p| ignores << p end
      else
        ignores << Regexp.quote(pat)
      end
    end
    if ignores.empty?
      @exclude_re = /^$/
    else
      re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|")
      @exclude_re = Regexp.new(re_str)
    end
  end

Clear all the exclude patterns so that we exclude nothing.

[Source]

# File lib/path_list.rb, line 199
  def clear_exclude
    @exclude_patterns = []
    calculate_exclude_regexp if ! @pending
  end

[Source]

# File lib/path_list.rb, line 51
  def clone
    sibling = self.class.new
    instance_variables.each do |ivar|
      value = self.instance_variable_get(ivar)
      sibling.instance_variable_set(ivar, value.try_dup)
    end
    sibling
  end
dup()

Alias for clone

[Source]

# File lib/path_list.rb, line 471
  def each ( &block )
    traditional_each do |x|
      if x.is_a? PathAndMatchData
        block[x, *x.match.to_a[1..-1]]
      else
        block[x]
      end
    end
  end

Register a list of file name patterns that should be excluded from the list. Patterns may be regular expressions, glob patterns or regular strings.

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:

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

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

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

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

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

[Source]

# File lib/path_list.rb, line 186
  def exclude(*patterns)
    patterns.each do |pat| @exclude_patterns << pat end
    if ! @pending
      calculate_exclude_regexp
      reject! { |fn| fn.to_s =~ @exclude_re }
    end
    self
  end

Should the given file name be excluded?

[Source]

# File lib/path_list.rb, line 414
  def exclude?(fn)
    calculate_exclude_regexp unless @exclude_re
# <<<<
    fn.to_s =~ @exclude_re
# >>>>
  end

Return a new array with Pathname#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.

[Source]

# File lib/path_list.rb, line 338
  def ext(newext='')
    collect { |fn| fn.ext(newext) }
  end

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

Example:

  PathList['lib/test/file', 'x/y'].gsub(/\//, "\\")
     => ['lib\\test\\file', 'x\\y']

[Source]

# File lib/path_list.rb, line 314
  def gsub(*args, &block)
    inject(PathList.new) { |res, fn| res << fn.gsub(*args, &block) }
  end

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

[Source]

# File lib/path_list.rb, line 325
  def gsub!(*args, &block)
    each_with_index { |fn, i| self[i] = fn.gsub(*args, &block) }
    self
  end

[Source]

# File lib/path_list.rb, line 431
  def import(array)
    @items = array
    self
  end

[Source]

# File lib/path_list.rb, line 481
  def import!
    traditional_each { |x| x.import! }
  end

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 )

[Source]

# File lib/path_list.rb, line 153
  def include(*filenames)
    # TODO: check for pending
    filenames.each do |fn|
      if fn.respond_to? :to_ary
        include(*fn.to_ary)
      else
        @pending_add << fn
      end
    end
    @pending = true
    self
  end

[Source]

# File lib/path_list.rb, line 485
  def load_path!
    traditional_each { |x| x.load_path! }
  end

Resolve all the pending adds now.

[Source]

# File lib/path_list.rb, line 233
  def resolve
    if @pending
      @pending = false
      @pending_add.each do |fn| resolve_add(fn) end
      @pending_add = []
      resolve_exclude
    end
    self
  end

[Source]

# File lib/path_list.rb, line 263
  def resolve_add(fn)
    case fn
    when Array
      fn.each { |f| self.resolve_add(f) }
    when %r{[*?\{]}
      add_matching(fn)
    else
# >>>>
      self << fn.to_path
# <<<<
    end
  end

[Source]

# File lib/path_list.rb, line 276
  def resolve_exclude
    @exclude_patterns.each do |pat|
      case pat
      when Regexp
# <<<<
        reject! { |fn| fn.to_s =~ pat }
# >>>>
      when /[*.]/
# <<<<
        reject_list = Pathname.glob(pat)
# >>>>
        reject! { |fn| reject_list.include?(fn) }
      else
# <<<<
        reject! { |fn| fn.to_s == pat.to_s }
# >>>>
      end
    end
    self
  end

[Source]

# File lib/path_list.rb, line 489
  def stringify
    map { |path| path.to_s }
  end

Return a new PathList with the results of running sub against each element of the oringal list.

Example:

  PathList['a.c', 'b.c'].sub(/\.c$/, '.o')  => ['a.o', 'b.o']

[Source]

# File lib/path_list.rb, line 303
  def sub(*args, &block)
    inject(PathList.new) { |res, fn| res << fn.sub(*args, &block) }
  end

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

[Source]

# File lib/path_list.rb, line 319
  def sub!(*args, &block)
    each_with_index { |fn, i| self[i] = fn.sub(*args, &block) }
    self
  end

Return the internal array object.

[Source]

# File lib/path_list.rb, line 210
  def to_a
    resolve
    @items
  end

Return the internal array object.

[Source]

# File lib/path_list.rb, line 216
  def to_ary
    resolve
    @items
  end

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

[Source]

# File lib/path_list.rb, line 355
  def to_s
    resolve if @pending
    self.join(' ')
  end

[Source]

# File lib/path_list.rb, line 366
  def to_yaml ( opts={} )
    resolve if @pending
    to_a.to_yaml(opts)
  end

Protected Instance methods

Add matching glob patterns.

[Source]

# File lib/path_list.rb, line 385
  def add_matching(pattern)
# <<<<
    pattern = pattern.to_s
    re = Regexp.quote(pattern)
    while re =~ /\\\{/
      re.gsub!(/\\\{([^{}]+)\\\}/) do
        '(?:' + $1.gsub(/,/, '|') + ')'
      end
    end
    re.gsub!(/\\\*\\\*\//, '(?:.+/)?')
    re.gsub!(/\\\*/, '[^/]*')
    re.gsub!(/\\\?/, '[^/]')
    re.gsub!(/\\([()])/, '\1')
    pattern.gsub!(/[()]/, '')
    Pathname.glob(pattern) do |fn|
# >>>>
      next if exclude?(fn)
      if match_data = Regexp.new(re).match(fn.to_s)
        self << PathAndMatchData.new(fn, match_data)
      else
        self << fn
      end
    end
  end

[Validate]