From d99c790f65204520690c20f5789148f3c7e2bce8 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 21 Mar 2012 11:12:46 -0700 Subject: split subscribers based on pattern type --- .../lib/active_support/notifications/fanout.rb | 44 +++++++++++++++------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/activesupport/lib/active_support/notifications/fanout.rb b/activesupport/lib/active_support/notifications/fanout.rb index 4c1b7b2784..bb1111e0c2 100644 --- a/activesupport/lib/active_support/notifications/fanout.rb +++ b/activesupport/lib/active_support/notifications/fanout.rb @@ -9,7 +9,7 @@ module ActiveSupport end def subscribe(pattern = nil, block = Proc.new) - subscriber = Subscriber.new(pattern, block) + subscriber = Subscribers.new pattern, block @subscribers << subscriber @listeners_for.clear subscriber @@ -36,23 +36,41 @@ module ActiveSupport def wait end - class Subscriber #:nodoc: - def initialize(pattern, delegate) - @pattern = pattern - @delegate = delegate + module Subscribers # :nodoc: + def self.new(pattern, block) + if pattern + Subscriber.new pattern, block + else + AllMessages.new pattern, block + end end - def publish(message, *args) - @delegate.call(message, *args) - end + class Subscriber #:nodoc: + def initialize(pattern, delegate) + @pattern = pattern + @delegate = delegate + end + + def publish(message, *args) + @delegate.call(message, *args) + end - def subscribed_to?(name) - !@pattern || @pattern === name.to_s + def subscribed_to?(name) + @pattern === name.to_s + end + + def matches?(subscriber_or_name) + self === subscriber_or_name || + @pattern && @pattern === subscriber_or_name + end end - def matches?(subscriber_or_name) - self === subscriber_or_name || - @pattern && @pattern === subscriber_or_name + class AllMessages < Subscriber # :nodoc: + def subscribed_to?(name) + true + end + + alias :matches? :=== end end end -- cgit v1.2.3