Class: FastIgnore::RuleSet
- Inherits:
-
Object
- Object
- FastIgnore::RuleSet
- Defined in:
- lib/fast_ignore/rule_set.rb
Instance Method Summary collapse
- #allowed_recursive?(relative_path, dir, full_path, filename, content = nil) ⇒ Boolean
- #allowed_unrecursive?(relative_path, dir, full_path, filename, content) ⇒ Boolean
- #empty? ⇒ Boolean
-
#initialize(rules, allow) ⇒ RuleSet
constructor
A new instance of RuleSet.
- #squash_rules(rules) ⇒ Object
- #weight ⇒ Object
Constructor Details
#initialize(rules, allow) ⇒ RuleSet
Returns a new instance of RuleSet.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/fast_ignore/rule_set.rb', line 5 def initialize(rules, allow) @dir_rules = squash_rules(rules.reject(&:file_only?)).freeze @file_rules = squash_rules(rules.reject(&:dir_only?)).freeze @any_not_anchored = rules.any?(&:unanchored?) @has_shebang_rules = rules.any?(&:shebang) @allowed_recursive = { '.' => true } @allow = allow freeze end |
Instance Method Details
#allowed_recursive?(relative_path, dir, full_path, filename, content = nil) ⇒ Boolean
17 18 19 20 21 22 23 |
# File 'lib/fast_ignore/rule_set.rb', line 17 def allowed_recursive?(relative_path, dir, full_path, filename, content = nil) @allowed_recursive.fetch(relative_path) do @allowed_recursive[relative_path] = allowed_recursive?(::File.dirname(relative_path), true, nil, nil, nil) && allowed_unrecursive?(relative_path, dir, full_path, filename, content) end end |
#allowed_unrecursive?(relative_path, dir, full_path, filename, content) ⇒ Boolean
25 26 27 28 29 30 31 |
# File 'lib/fast_ignore/rule_set.rb', line 25 def allowed_unrecursive?(relative_path, dir, full_path, filename, content) (dir ? @dir_rules : @file_rules).reverse_each do |rule| return rule.negation? if rule.match?(relative_path, full_path, filename, content) end (not @allow) || (dir && @any_not_anchored) end |
#empty? ⇒ Boolean
47 48 49 |
# File 'lib/fast_ignore/rule_set.rb', line 47 def empty? @dir_rules.empty? && @file_rules.empty? end |
#squash_rules(rules) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/fast_ignore/rule_set.rb', line 33 def squash_rules(rules) out = rules.chunk_while { |a, b| a.type == b.type }.map do |chunk| next chunk.first if chunk.length == 1 chunk.first.class.new(Regexp.union(chunk.map(&:rule)), chunk.first.negation?) end out end |
#weight ⇒ Object
43 44 45 |
# File 'lib/fast_ignore/rule_set.rb', line 43 def weight @dir_rules.length + (@has_shebang_rules ? 10 : 0) end |