class TrieMatcher::PatternMatcher

Convenience class for matching specific patterns, accelerated by a static prefix.

This is extremely useful for matching complex patterns such as user agents or url routes

Public Class Methods

new() click to toggle source

Build an empty pattern matcher

# File lib/trie_matcher/pattern_matcher.rb, line 8
def initialize
  @trie = TrieMatcher.new
end

Public Instance Methods

add_pattern(prefix, pattern, &block) click to toggle source

Register a pattern, along with a static prefix

@param prefix [String] a static prefix that indicates this pattern should be tested @param pattern [Regexp] a pattern to test against @yield [match] executed if a positive match is made @yieldparam match [MatchData] the match data from the pattern

# File lib/trie_matcher/pattern_matcher.rb, line 18
def add_pattern(prefix, pattern, &block)
  @trie[prefix] ||= {}
  @trie[prefix][pattern] = block
end
match(string) click to toggle source

Match a string against all registered patterns efficiently.

Calls the block registered against any matching pattern, and passes in the match data

@param string [String] the string to match against

# File lib/trie_matcher/pattern_matcher.rb, line 28
def match(string)
  result = @trie[string]
  return nil unless result
  result.each do |pattern, block|
    match = pattern.match(string)
    block.call(match) if match
  end
end