require 'dry-equalizer' module Messaging module Routing # Internal: Used by routing to match a pattern against a callable (handler) class Route include Dry::Equalizer(:matcher, :handler) attr_reader :matcher attr_reader :handler def initialize(pattern, handler) @matcher = MessageMatcher.new(pattern: pattern) @handler = handler verify_handler! end def call(message, context = self) return unless @matcher.matches?(message) context.instance_exec(message, &@handler) end def topics matcher.topics end private def verify_handler! raise ArgumentError, 'Handler must be callable' unless handler.respond_to? :call end end end end