diff options
author | David Chelimsky <dchelimsky@gmail.com> | 2010-04-24 21:12:02 -0500 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-04-24 22:06:24 -0700 |
commit | 864bd9c21fdc8ce265c48fdfa2d4d7917032e717 (patch) | |
tree | aff882b58a30b4c7b02e521564dba3ce672fa8a8 /activesupport/lib | |
parent | df886c4c89f3b8a95ef2456d6893393376b330a9 (diff) | |
download | rails-864bd9c21fdc8ce265c48fdfa2d4d7917032e717.tar.gz rails-864bd9c21fdc8ce265c48fdfa2d4d7917032e717.tar.bz2 rails-864bd9c21fdc8ce265c48fdfa2d4d7917032e717.zip |
allow unsubscribe by name or subscription [#4433 state:resolved]
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/notifications/fanout.rb | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/notifications/fanout.rb b/activesupport/lib/active_support/notifications/fanout.rb index a3ddc7705a..300ec842a9 100644 --- a/activesupport/lib/active_support/notifications/fanout.rb +++ b/activesupport/lib/active_support/notifications/fanout.rb @@ -19,8 +19,8 @@ module ActiveSupport end def unsubscribe(subscriber) - @subscribers.delete(subscriber) @listeners_for.clear + @subscribers.reject! {|s| s.matches?(subscriber)} end def publish(name, *args) @@ -60,7 +60,7 @@ module ActiveSupport end def publish(*args) - return unless matches?(args.first) + return unless subscribed_to?(args.first) push(*args) true end @@ -69,10 +69,20 @@ module ActiveSupport true end - private - def matches?(name) - !@pattern || @pattern =~ name.to_s + def subscribed_to?(name) + !@pattern || @pattern =~ name.to_s + end + + def matches?(subscriber_or_name) + case subscriber_or_name + when String + @pattern && @pattern =~ subscriber_or_name + when self + true end + end + + private def push(*args) @block.call(*args) |