class Ergo::Ignore

Encapsulates list of file globs to be ignored.

Public Class Methods

new(options={}) click to toggle source

Initialize new instance of Ignore.

Returns nothing.

# File lib/ergo/ignore.rb, line 16
def initialize(options={})
  @file = options[:file]
  @root = options[:root]

  @ignore = load_ignore
end

Public Instance Methods

concat(*globs) click to toggle source
# File lib/ergo/ignore.rb, line 73
def concat(*globs)
  @ignore.concat(globs.flatten)
end
each() { |g| ... } click to toggle source
# File lib/ergo/ignore.rb, line 53
def each
  to_a.each{ |g| yield g }
end
file() click to toggle source

Ignore file.

# File lib/ergo/ignore.rb, line 46
def file
  @file ||= (
    Dir["{.gitignore,.hgignore,#{IGNORE_FILE}}"].first
  )
end
filter(files) click to toggle source

Filter a list of files in accordance with the ignore list.

files - The list of files. [Array<String>]

Returns [Array<String>]

# File lib/ergo/ignore.rb, line 29
def filter(files)
  list = []
  files.each do |file|
    hit = any? do |pattern|
      match?(pattern, file)
    end
    list << file unless hit
  end
  list
end
fnmatch?(pattern, file, mode=File::FNM_PATHNAME) click to toggle source

Shortcut to ‘File.fnmatch?` method.

Returns [Boolean]

# File lib/ergo/ignore.rb, line 140
def fnmatch?(pattern, file, mode=File::FNM_PATHNAME)
  File.fnmatch?(pattern, file, File::FNM_PATHNAME)
end
load_ignore() click to toggle source

Load ignore file. Removes blank lines and line starting with ‘#`.

Returns [Array<String>]

# File lib/ergo/ignore.rb, line 94
def load_ignore
  f = file
  i = []
  if f && File.exist?(f)
    File.read(f).lines.each do |line|
      glob = line.strip
      next if glob.empty?
      next if glob.start_with?('#')
      i << glob
    end
  end
  i
end
match?(pattern, file) click to toggle source

Given a pattern and a file, does the file match the pattern? This code is based on the rules used by git’s .gitignore file.

TODO: The code is probably not quite right.

Returns [Boolean]

# File lib/ergo/ignore.rb, line 115
def match?(pattern, file)
  if pattern.start_with?('!')
    return !match?(pattern.sub('!','').strip)
  end

  dir = pattern.end_with?('/')
  pattern = pattern.chomp('/') if dir

  if pattern.start_with?('/')
    fnmatch?(pattern.sub('/',''), file)
  else
    if dir
      fnmatch?(File.join(pattern, '**', '*'), file) ||
      fnmatch?(pattern, file) && File.directory?(file)
    elsif pattern.include?('/')
      fnmatch?(pattern, file)
    else
      fnmatch?(File.join('**',pattern), file)
    end
  end
end
replace(*globs) click to toggle source
# File lib/ergo/ignore.rb, line 68
def replace(*globs)
  @ignore = globs.flatten
end
size() click to toggle source
# File lib/ergo/ignore.rb, line 58
def size
  to_a.size
end
to_a() click to toggle source
# File lib/ergo/ignore.rb, line 63
def to_a
  @ignore #||= load_ignore
end