Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add an unsubscribe_all method

  • Loading branch information...
commit dd3caa86481e6604d2a40bd90d4b952e1f4d7f15 1 parent 7f82e1b
@ttencate authored
Showing with 37 additions and 0 deletions.
  1. +10 −0 lib/publisher.rb
  2. +27 −0 test/publisher_test.rb
View
10 lib/publisher.rb
@@ -71,6 +71,16 @@ def unsubscribe(event, listener)
end
end
+ # Unsubscribe from all events. Like calling 'unsubscribe' for all events that
+ # 'listener' has subscribed to.
+ def unsubscribe_all(listener)
+ if @subscriptions
+ @subscriptions.keys.each do |event|
+ unsubscribe(event, listener)
+ end
+ end
+ end
+
protected
# Fire an event with 0 or more outbound parameters
def fire(event, *args) #:nod
View
27 test/publisher_test.rb
@@ -92,6 +92,33 @@ def test_unsubscribe_removes_all_subscriptions_for_the_event_being_listened_for
assert_equal ['a','b','c'], out
end
+ def test_unsubscribe_all_unsubscribes_the_unsubscriber_from_all_events
+ something = Something.new
+ watcher = SomethingWatcher.new something
+
+ out = []
+ something.on :boom do
+ out << 'a'
+ end
+ something.on :boom do
+ out << 'b'
+ end
+ something.on :pow do
+ out << 'c'
+ end
+
+ something.do_boom
+ something.do_pow
+ assert_equal ['a','b','c'], out
+ assert_equal ['boom'], watcher.observations
+
+ something.unsubscribe_all self
+ something.do_boom
+ something.do_pow
+ assert_equal ['a','b','c'], out
+ assert_equal ['boom','boom'], watcher.observations
+ end
+
def test_subscribe_and_fire
obj = Something.new
Please sign in to comment.
Something went wrong with that request. Please try again.