class PathList::Matchers::WithinDir

Public Class Methods

new(matchers, root) click to toggle source
# File lib/path_list/matchers/within_dir.rb, line 6
def initialize(matchers, root)
  @dir_matchers = squash_matchers(matchers.reject(&:file_only?))
  @file_matchers = squash_matchers(matchers.reject(&:dir_only?))
  @has_shebang_matchers = matchers.any?(&:shebang?)
  @root = root

  freeze
end

Public Instance Methods

empty?() click to toggle source
# File lib/path_list/matchers/within_dir.rb, line 27
def empty?
  @dir_matchers.empty? && @file_matchers.empty?
end
match?(root_candidate) click to toggle source
# File lib/path_list/matchers/within_dir.rb, line 15
def match?(root_candidate)
  relative_candidate = root_candidate.relative_to(@root)
  return false unless relative_candidate

  (root_candidate.directory? ? @dir_matchers : @file_matchers).reverse_each do |rule|
    val = rule.match?(relative_candidate)
    return val if val
  end

  false
end
weight() click to toggle source
# File lib/path_list/matchers/within_dir.rb, line 31
def weight
  @dir_matchers.length + (@has_shebang_matchers ? 10 : 0)
end

Private Instance Methods

squash_matchers(matchers) click to toggle source
# File lib/path_list/matchers/within_dir.rb, line 37
def squash_matchers(matchers)
  return matchers if matchers.empty?

  matchers -= [::PathList::Matchers::Unmatchable]
  return [::PathList::Matchers::Unmatchable] if matchers.empty?

  matchers.chunk_while { |a, b| a.squash_id == b.squash_id }.map do |chunk|
    next ::PathList::Matchers::AllowAnyDir if chunk.include?(::PathList::Matchers::AllowAnyDir)

    chunk.uniq!(&:rule)
    next chunk.first if chunk.length == 1

    chunk.first.squash(chunk)
  end
end