# frozen_string_literal: true module Fimon class Matcher def initialize(patterns) @patterns = Array(patterns).map {|pattern| to_regex(pattern) } end def match?(input) @patterns.any? {|pattern| pattern.match?(input) } end alias =~ match? private def to_regex(pattern) double = "__double__" single = "__single__" extension_groups = pattern.scan(/\{(.*?)\}/).flatten extension_groups.each_with_index do |group, index| pattern = pattern.gsub("{#{group}}", "__#{index}__") end pattern = pattern .gsub("**/", double) .gsub("*", single) pattern = Regexp.escape(pattern) .gsub(double, "([^/]+/)*") .gsub(single, "[^/]+") extension_groups.each_with_index do |group, i| pattern = pattern.gsub("__#{i}__", "(#{group.split(',').join('|')})") end Regexp.new("^#{pattern}$") end end end