Navigation Menu

Skip to content

Commit

Permalink
split subscribers based on pattern type
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Mar 21, 2012
1 parent 14b2cf6 commit d99c790
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions activesupport/lib/active_support/notifications/fanout.rb
Expand Up @@ -9,7 +9,7 @@ def initialize
end

def subscribe(pattern = nil, block = Proc.new)
subscriber = Subscriber.new(pattern, block)
subscriber = Subscribers.new pattern, block
@subscribers << subscriber
@listeners_for.clear
subscriber
Expand All @@ -36,23 +36,41 @@ def listening?(name)
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
Expand Down

0 comments on commit d99c790

Please sign in to comment.