File: matcher.rb

package info (click to toggle)
ruby-glob 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 200 kB
  • sloc: ruby: 449; makefile: 7; sh: 4
file content (36 lines) | stat: -rw-r--r-- 712 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# frozen_string_literal: true

module Glob
  class Matcher
    attr_reader :path, :regex

    def initialize(path)
      @path = path
      @reject = path.start_with?("!")

      pattern = Regexp.escape(path.gsub(/^!/, ""))
                      .gsub(/(\\{.*?\\})/) {|match| process_group(match) }
                      .gsub("\\*", "[^.]+")
      anchor = path.end_with?("*") ? "" : "$"
      @regex = Regexp.new("^#{pattern}#{anchor}")
    end

    def match?(other)
      other.match?(regex)
    end

    def include?
      !reject?
    end

    def reject?
      @reject
    end

    def process_group(group)
      group = group.gsub(/[{}\\]/, "").split(",").join("|")

      "(#{group})"
    end
  end
end