@@ -20,11 +20,13 @@ def initialize
20
20
# or a falsey value otherwise.
21
21
#
22
22
def register_matchers ( *matchers , handler )
23
+ fail ArgumentError , 'handler is required' if handler . nil?
24
+
23
25
matchers . each do |matcher |
24
26
if matcher . is_a? ( MessageMatchers ::InstanceOf )
25
- @instanceof_routes [ matcher . expected_class ] << handler
27
+ ( @instanceof_routes [ matcher . expected_class ] ||= Set . new ) << handler
26
28
else
27
- @routes [ matcher ] << handler
29
+ ( @routes [ matcher ] ||= Set . new ) << handler
28
30
end
29
31
end
30
32
end
@@ -34,7 +36,7 @@ def register_matchers(*matchers, handler)
34
36
#
35
37
def match_message ( message )
36
38
result = Set . new
37
- result . merge ( @instanceof_routes [ message . class ] )
39
+ result . merge ( @instanceof_routes [ message . class ] ) if @instanceof_routes . include? ( message . class )
38
40
@routes . each do |matcher , handlers |
39
41
result . merge ( handlers ) if matcher . matches_message? ( message )
40
42
end
@@ -45,15 +47,16 @@ def match_message(message)
45
47
# Returns true when there is at least one handler for the given message, or false otherwise.
46
48
#
47
49
def matches_message? ( message )
48
- match_message ( message ) . any?
50
+ @instanceof_routes . include? ( message . class ) ||
51
+ @routes . keys . any? { |matcher | matcher . matches_message? ( message ) }
49
52
end
50
53
51
54
##
52
55
# Removes all routes from the router.
53
56
#
54
57
def clear_routes
55
- @instanceof_routes = Hash . new { | h , k | h [ k ] = Set . new }
56
- @routes = Hash . new { | h , k | h [ k ] = Set . new }
58
+ @instanceof_routes = { }
59
+ @routes = { }
57
60
end
58
61
end
59
62
end
0 commit comments