Class: Mailman::Route::StringMatcher
- 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
-
#keys ⇒ Array<Symbol>
readonly
The names of the param captures.
Attributes inherited from Matcher
Class Method Summary collapse
Instance Method Summary collapse
- #compile! ⇒ Object
-
#match(string) ⇒ ({Symbol => String}, <String>)
Matches against a string using the stored pattern.
Methods inherited from Matcher
create, inherited, #initialize
Constructor Details
This class inherits a constructor from Mailman::Route::Matcher
Instance Attribute Details
#keys ⇒ Array<Symbol> (readonly)
Returns 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
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.
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 |