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
|