Class: Mailman::Route::StringMatcher

Inherits:
Matcher
  • Object
show all
Defined in:
lib/mailman/route/string_matcher.rb

Overview

Matches using a String with named param captures formatted like %user%@example.com.

Instance Attribute Summary collapse

Attributes inherited from Matcher

#pattern

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Matcher

create, inherited, #initialize

Constructor Details

This class inherits a constructor from Mailman::Route::Matcher

Instance Attribute Details

#keysArray<Symbol> (readonly)

Returns the names of the param captures.

Returns:

  • (Array<Symbol>)

    the names of the param captures


8
9
10
# File 'lib/mailman/route/string_matcher.rb', line 8

def keys
  @keys
end

Class Method Details

.valid_pattern?(pattern) ⇒ Boolean

Returns:

  • (Boolean)

38
39
40
# File 'lib/mailman/route/string_matcher.rb', line 38

def self.valid_pattern?(pattern)
  pattern.respond_to?(:to_s)
end

Instance Method Details

#compile!Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mailman/route/string_matcher.rb', line 23

def compile!
  @keys = []
  special_chars = %w/* . + ? \\ | ^ $ ( ) [ ] { } /
  compiled_pattern = @pattern.to_s.gsub(/((%[A-Za-z_]+%)|[\*\\.+?|^$()\[\]{}])/) do |match|
    case match
    when *special_chars
      Regexp.escape(match)
    else
      @keys << $2[1..-2].to_sym
      '(.*)'
    end
  end
  @pattern = /#{compiled_pattern}/i
end

#match(string) ⇒ ({Symbol => String}, <String>)

Matches against a string using the stored pattern.

Parameters:

  • string (String)

    the string to match against

Returns:

  • (({Symbol => String}, <String>))

    the params hash, and array of captures.


14
15
16
17
18
19
20
21
# File 'lib/mailman/route/string_matcher.rb', line 14

def match(string)
  params = {}
  if match = @pattern.match(string)
    captures = match.captures
    params.merge!(Hash[*@keys.zip(captures).flatten])
    [params, captures]
  end
end