Class: Ergo::Ignore
Overview
Encapsulates list of file globs to be ignored.
Instance Method Summary collapse
- #concat(*globs) ⇒ Object
- #each ⇒ Object
-
#file ⇒ Object
Ignore file.
-
#filter(files) ⇒ Array<String>
Filter a list of files in accordance with the ignore list.
-
#fnmatch?(pattern, file, mode = File::FNM_PATHNAME) ⇒ Boolean
Shortcut to ‘File.fnmatch?` method.
-
#initialize(options = {}) ⇒ void
constructor
Initialize new instance of Ignore.
-
#load_ignore ⇒ Array<String>
Load ignore file.
-
#match?(pattern, file) ⇒ Boolean
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.
- #replace(*globs) ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ void
Initialize new instance of Ignore.
16 17 18 19 20 21 |
# File 'lib/ergo/ignore.rb', line 16 def initialize(={}) @file = [:file] @root = [:root] @ignore = load_ignore end |
Instance Method Details
#concat(*globs) ⇒ Object
73 74 75 |
# File 'lib/ergo/ignore.rb', line 73 def concat(*globs) @ignore.concat(globs.flatten) end |
#each ⇒ Object
53 54 55 |
# File 'lib/ergo/ignore.rb', line 53 def each to_a.each{ |g| yield g } end |
#file ⇒ Object
Ignore file.
46 47 48 49 50 |
# File 'lib/ergo/ignore.rb', line 46 def file @file ||= ( Dir["{.gitignore,.hgignore,#{IGNORE_FILE}}"].first ) end |
#filter(files) ⇒ Array<String>
Filter a list of files in accordance with the ignore list.
29 30 31 32 33 34 35 36 37 38 |
# 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) ⇒ Boolean
Shortcut to ‘File.fnmatch?` method.
140 141 142 |
# 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 ⇒ Array<String>
Load ignore file. Removes blank lines and line starting with ‘#`.
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# 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) ⇒ Boolean
TODO:
The code is probably not quite right.
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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# 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) ⇒ Object
68 69 70 |
# File 'lib/ergo/ignore.rb', line 68 def replace(*globs) @ignore = globs.flatten end |
#size ⇒ Object
58 59 60 |
# File 'lib/ergo/ignore.rb', line 58 def size to_a.size end |
#to_a ⇒ Object
63 64 65 |
# File 'lib/ergo/ignore.rb', line 63 def to_a @ignore #||= load_ignore end |