Class: Buff::Ignore::IgnoreFile
- Inherits:
-
Object
- Object
- Buff::Ignore::IgnoreFile
- Defined in:
- lib/buff/ignore/ignore_file.rb
Overview
A Ruby representation of an ignore file
Constant Summary collapse
- COMMENT_OR_WHITESPACE =
Regular expression to match comments or plain whitespace
/^\s*(?:#.*)?$/.freeze
Instance Attribute Summary collapse
-
#filepath ⇒ String
readonly
The path to the ignore file.
Instance Method Summary collapse
-
#apply(list) ⇒ Array
Apply the ignore to the list, returning a new list of filtered files.
-
#apply!(list) ⇒ Array?
Destructively remove all files from the given list.
-
#ignored?(filename) ⇒ Boolean
Determine if a given filename should be ignored.
-
#initialize(filepath, options = {}) ⇒ IgnoreFile
constructor
Create a new ignore file from the given filepath.
Constructor Details
#initialize(filepath, options = {}) ⇒ IgnoreFile
Create a new ignore file from the given filepath
27 28 29 30 31 32 33 34 35 |
# File 'lib/buff/ignore/ignore_file.rb', line 27 def initialize(filepath, = {}) raise IgnoreFileNotFound.new(filepath) unless filepath && File.exists?(filepath) @filepath = File.(filepath) @options = if @options[:base].nil? @options[:base] = File.directory?(filepath) ? filepath : File.dirname(filepath) end end |
Instance Attribute Details
#filepath ⇒ String (readonly)
The path to the ignore file
13 14 15 |
# File 'lib/buff/ignore/ignore_file.rb', line 13 def filepath @filepath end |
Instance Method Details
#apply(list) ⇒ Array
Apply the ignore to the list, returning a new list of filtered files
50 51 52 53 54 |
# File 'lib/buff/ignore/ignore_file.rb', line 50 def apply(list) tmp = list.dup apply!(tmp) tmp end |
#apply!(list) ⇒ Array?
Destructively remove all files from the given list
63 64 65 66 67 |
# 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) ⇒ Boolean
Determine if a given filename should be ignored
76 77 78 79 80 81 |
# File 'lib/buff/ignore/ignore_file.rb', line 76 def ignored?(filename) base = File.([:base] || File.dirname(filepath)) basename = filename.sub(base + File::SEPARATOR, '') ignores.any? { |ignore| File.fnmatch?(ignore, basename) } end |