Class: Rounders::Matchers::Matcher

Inherits:
Object
  • Object
show all
Includes:
Plugins::Pluggable
Defined in:
lib/rounders/matchers/matcher.rb

Direct Known Subclasses

Body, CC, From, Subject, To

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plugins::Pluggable

included

Constructor Details

#initialize(matchers) ⇒ Matcher

Returns a new instance of Matcher.



10
11
12
# File 'lib/rounders/matchers/matcher.rb', line 10

def initialize(matchers)
  @matchers = matchers
end

Instance Attribute Details

#matchersObject (readonly)

Returns the value of attribute matchers.



8
9
10
# File 'lib/rounders/matchers/matcher.rb', line 8

def matchers
  @matchers
end

Class Method Details

.build(conditions) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/rounders/matchers/matcher.rb', line 31

def build(conditions)
  matchers = conditions.map do |key, pattern|
    matcher = Rounders.matchers[key]
    raise Rounders::Matchers::NoImplementError if matcher.nil?
    matcher.new(pattern)
  end
  new(matchers)
rescue StandardError => e
  raise e
end

.inherited(klass) ⇒ Object



23
24
25
# File 'lib/rounders/matchers/matcher.rb', line 23

def inherited(klass)
  Rounders.matchers[klass.symbol] = klass
end

.symbolObject



27
28
29
# File 'lib/rounders/matchers/matcher.rb', line 27

def symbol
  Util.infrect(name.split('::').last).underscore.to_sym
end

Instance Method Details

#match(message) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/rounders/matchers/matcher.rb', line 14

def match(message)
  match_data = matchers.each_with_object({}) do |matcher, memo|
    memo[matcher.class.symbol] = matcher.match(message)
  end
  return match_data if match_data.values.none? { |value| Util.blank?(value) }
  nil
end