Class: Ace::Support::Config::Atoms::PathRuleMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/support/config/atoms/path_rule_matcher.rb

Overview

Match file paths against path-based config rules

Defined Under Namespace

Classes: MatchResult

Instance Method Summary collapse

Constructor Details

#initialize(path_rules, project_root: nil) ⇒ PathRuleMatcher

Returns a new instance of PathRuleMatcher.

Parameters:

  • path_rules (Hash)

    Path rules with optional _config_root metadata

  • project_root (String, nil) (defaults to: nil)

    Project root for relative path calculation



13
14
15
16
# File 'lib/ace/support/config/atoms/path_rule_matcher.rb', line 13

def initialize(path_rules, project_root: nil)
  @path_rules = path_rules || {}
  @project_root = project_root
end

Instance Method Details

#match(file_path) ⇒ MatchResult?

Match file path against configured rules

Parameters:

  • file_path (String)

    File path relative to project root

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ace/support/config/atoms/path_rule_matcher.rb', line 21

def match(file_path)
  return nil if @path_rules.nil? || @path_rules.empty?
  return nil if file_path.nil? || file_path.to_s.empty?

  normalized = normalize_path(file_path)

  @path_rules.each do |name, rule|
    next unless rule.is_a?(Hash)

    glob = rule["glob"] || rule[:glob]
    globs = Array(glob).compact.map(&:to_s).reject(&:empty?)
    next if globs.empty?

    # Calculate path relative to rule's config root
    # Returns nil if file is outside config root's scope
    path_to_match = path_relative_to_config(normalized, rule)
    next if path_to_match.nil?

    globs.each do |glob_pattern|
      next unless File.fnmatch?(glob_pattern, path_to_match, match_flags(glob_pattern))

      return MatchResult.new(
        name: name.to_s,
        config: extract_config(rule)
      )
    end
  end

  nil
end