Class: Messaging::Routing::MessageMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/messaging/routing/message_matcher.rb

Overview

Internal: Used by subscribers to match messages.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern:) ⇒ MessageMatcher

Returns a new instance of MessageMatcher.



10
11
12
# File 'lib/messaging/routing/message_matcher.rb', line 10

def initialize(pattern:)
  self.pattern = pattern
end

Instance Attribute Details

#patternObject

Returns the value of attribute pattern.



8
9
10
# File 'lib/messaging/routing/message_matcher.rb', line 8

def pattern
  @pattern
end

Instance Method Details

#all_matching_messagesObject



36
37
38
# File 'lib/messaging/routing/message_matcher.rb', line 36

def all_matching_messages
  Messaging.defined_messages.select(&method(:matches?))
end

#call(message) ⇒ Object



32
33
34
# File 'lib/messaging/routing/message_matcher.rb', line 32

def call(message)
  matches?(message)
end

#categoriesObject



47
48
49
# File 'lib/messaging/routing/message_matcher.rb', line 47

def categories
  all_matching_messages.map { |m| m.new.stream_category }.uniq
end

#matches?(message) ⇒ Boolean

Internal: See routing.rb for examples on how it is used.

Returns:

  • (Boolean)


15
16
17
# File 'lib/messaging/routing/message_matcher.rb', line 15

def matches?(message)
  matches_pattern?(message)
end

#matches_pattern?(message) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/messaging/routing/message_matcher.rb', line 19

def matches_pattern?(message)
  case pattern
  when Regexp
    pattern =~ message.message_type
  when Class, Module
    /^#{pattern}/ =~ message.message_type
  when Proc
    pattern.call(message)
  else
    false
  end
end

#topicsObject

Internal: Get all topics that the matcher would need to match a message.

Used by the Kafka adapter to setup consumers.



43
44
45
# File 'lib/messaging/routing/message_matcher.rb', line 43

def topics
  all_matching_messages.map(&:topic).uniq
end