Class: Mailman::Route::Matcher

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

Overview

The base matcher class. All matchers should subclass and override #match and Matcher.valid_pattern?. Override #compile! if a pattern compiler is needed.

Direct Known Subclasses

RegexpMatcher, StringMatcher

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Matcher

Creates a new matcher and calls #compile!.

Parameters:

  • pattern

    the matcher pattern


13
14
15
16
# File 'lib/mailman/route/matcher.rb', line 13

def initialize(pattern)
  @pattern = pattern
  compile!
end

Class Attribute Details

.matchers<Class> (readonly)

Returns The array of registered matchers.

Returns:

  • (<Class>)

    The array of registered matchers.


34
35
36
# File 'lib/mailman/route/matcher.rb', line 34

def matchers
  @matchers
end

Instance Attribute Details

#patternObject (readonly)

Returns the matcher pattern, normally stored as a Regexp.

Returns:

  • the matcher pattern, normally stored as a Regexp.


9
10
11
# File 'lib/mailman/route/matcher.rb', line 9

def pattern
  @pattern
end

Class Method Details

.create(pattern) ⇒ Object

Finds and creates a valid matcher instance for a given pattern.

Parameters:

  • pattern

    the pattern to create the matcher with


45
46
47
48
49
# File 'lib/mailman/route/matcher.rb', line 45

def create(pattern)
  @matchers.each do |matcher|
    return matcher.new(pattern) if matcher.valid_pattern?(pattern)
  end
end

.inherited(matcher) ⇒ Object

Registers a matcher so that it can be used in create.

Parameters:

  • matcher (Class)

    a matcher subclass


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

def inherited(matcher)
  @matchers ||= []
  @matchers << matcher
end

.valid_pattern?(pattern) ⇒ true, false

This method is abstract.

Checks whether a pattern is valid.

Parameters:

  • pattern

    the pattern to check

Returns:

  • (true, false)

Raises:

  • (NotImplementedError)

54
55
56
# File 'lib/mailman/route/matcher.rb', line 54

def valid_pattern?(pattern)
  raise NotImplementedError
end

Instance Method Details

#compile!Object

This method is abstract.

Compiles the pattern into something easier to work with, usually a Regexp.


28
29
# File 'lib/mailman/route/matcher.rb', line 28

def compile!
end

#match(string) ⇒ (Hash, Array)

This method is abstract.

Matches a string against the stored pattern.

Returns a hash to merge into params, and an array of arguments for the block.

Parameters:

  • string (String)

    the string to match against

Returns:

  • ((Hash, Array))

    a hash to merge into params, and an array of arguments for the block.

Raises:

  • (NotImplementedError)

22
23
24
# File 'lib/mailman/route/matcher.rb', line 22

def match(string)
  raise NotImplementedError
end