Sha256: ec2049fb583b1d232e203f73887fc9e57e725f48186f5e260d8fae1ba605b960

Contents?: true

Size: 712 Bytes

Versions: 1

Compression:

Stored size: 712 Bytes

Contents

# 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
glob-0.4.1 lib/glob/matcher.rb