Sha256: fe134fe361a75ba40e44b10097afd385ff96b451b21d0d3bf6616dc3dac667f6

Contents?: true

Size: 1.09 KB

Versions: 19

Compression:

Stored size: 1.09 KB

Contents

require 'dry-equalizer'
module Messaging
  module Routing
    # Internal: Used by subscribers to match messages.
    class MessageMatcher
      include Dry::Equalizer(:pattern)

      attr_accessor :pattern

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

      # Internal: See routing.rb for examples on how it is used.
      def matches?(message)
        matches_pattern?(message)
      end

      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

      def call(message)
        matches?(message)
      end

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

      # Internal: Get all topics that the matcher would need to match a message.
      #
      # Used by the Kafka adapter to setup consumers.
      def topics
        all_matching_messages.map(&:topic).uniq
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
messaging-3.8.2 lib/messaging/routing/message_matcher.rb
messaging-3.8.1 lib/messaging/routing/message_matcher.rb
messaging-3.8.0 lib/messaging/routing/message_matcher.rb
messaging-3.7.3 lib/messaging/routing/message_matcher.rb
messaging-3.7.2 lib/messaging/routing/message_matcher.rb
messaging-3.7.1 lib/messaging/routing/message_matcher.rb
messaging-3.7.0 lib/messaging/routing/message_matcher.rb
messaging-3.6.2 lib/messaging/routing/message_matcher.rb
messaging-3.6.1 lib/messaging/routing/message_matcher.rb
messaging-3.6.0 lib/messaging/routing/message_matcher.rb
messaging-3.5.7 lib/messaging/routing/message_matcher.rb
messaging-3.5.6 lib/messaging/routing/message_matcher.rb
messaging-3.5.5 lib/messaging/routing/message_matcher.rb
messaging-3.5.4 lib/messaging/routing/message_matcher.rb
messaging-3.5.3 lib/messaging/routing/message_matcher.rb
messaging-3.5.2 lib/messaging/routing/message_matcher.rb
messaging-3.5.1 lib/messaging/routing/message_matcher.rb
messaging-3.4.3 lib/messaging/routing/message_matcher.rb
messaging-3.4.2 lib/messaging/routing/message_matcher.rb