Sha256: ae869d1e36c983b15eb82c00107b0aa9d74a6dbd6f49dc1d4f54ac41dd054e96

Contents?: true

Size: 658 Bytes

Versions: 3

Compression:

Stored size: 658 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(/\\\*/, "[^.]+")
      @regex = Regexp.new("^#{pattern}")
    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

3 entries across 3 versions & 1 rubygems

Version Path
glob-0.4.0 lib/glob/matcher.rb
glob-0.3.1 lib/glob/matcher.rb
glob-0.3.0 lib/glob/matcher.rb