class Buff::Ignore::IgnoreFile

A Ruby representation of an ignore file

Constants

COMMENT_OR_WHITESPACE

Regular expression to match comments or plain whitespace

@return [Regexp]

Attributes

filepath[R]

The path to the ignore file

@return [String]

options[R]

The list of options

@return [Hash]

Public Class Methods

new(filepath, options = {}) click to toggle source

Create a new ignore file from the given filepath

@raise [IgnoreFileNotFound]

if the given filepath does not exist

@param [String, Pathname] filepath

the path to the ignore file

@param [Hash] options

a list of options to pass to the ignore file

@option [#to_s] options :base

the base directory to apply ignores from
# File lib/buff/ignore/ignore_file.rb, line 27
def initialize(filepath, options = {})
  raise IgnoreFileNotFound.new(filepath) unless filepath && File.exists?(filepath)

  @filepath = File.expand_path(filepath)
  @options  = options
  if @options[:base].nil?
    @options[:base] = File.directory?(filepath) ? filepath : File.dirname(filepath)
  end
end

Public Instance Methods

apply(list) click to toggle source

Apply the ignore to the list, returning a new list of filtered files

@example

files = ['Gemfile', 'Gemfile.lock', 'bacon', 'eggs']
ignore.apply(files) #=> ['bacon', 'eggs']

@see IgnoreFile#apply!

@param [Array] list

the list of files to apply the ignore to

@return [Array]

the sanitized file list
# File lib/buff/ignore/ignore_file.rb, line 50
def apply(list)
  tmp = list.dup
  apply!(tmp)
  tmp
end
apply!(list) click to toggle source

Destructively remove all files from the given list

@param [Array] list

the list of files to apply the ignore to

@return [Array, nil]

the elements removed, or nil if none were removed
# File lib/buff/ignore/ignore_file.rb, line 63
def apply!(list)
  list.reject! do |item|
    item.strip.empty? || ignored?(item)
  end
end
ignored?(filename) click to toggle source

Determine if a given filename should be ignored

@param [String] filename

the file to match

@return [Boolean]

true if the file should be ignored, false otherwise
# File lib/buff/ignore/ignore_file.rb, line 76
def ignored?(filename)
  base = File.expand_path(options[:base] || File.dirname(filepath))
  basename = filename.sub(base + File::SEPARATOR, '')

  ignores.any? { |ignore| File.fnmatch?(ignore, basename) }
end

Private Instance Methods

ignores() click to toggle source

The parsed contents of the ignore file

@return [Array]

# File lib/buff/ignore/ignore_file.rb, line 92
def ignores
  @ignores ||= File.readlines(filepath).map(&:strip).reject do |line|
    line.empty? || line =~ COMMENT_OR_WHITESPACE
  end
end