Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Version 1.1.1 - supports inheritance of events

git-svn-id: http://atomicobjectrb.rubyforge.org/svn/trunk/publisher@74 7bd720c3-caf9-44c9-a48d-612f81e63739
  • Loading branch information...
commit 3e34f7eeca66cb97f7e6741daac2e374cce8e3f5 1 parent e1b6a9f
dcrosby42 authored
View
4 History.txt
@@ -1,3 +1,7 @@
+== 1.1.1 / 2007-12-28
+
+* Subclasses now inherit the events supported by their ancestors
+
== 1.1.0 / 2007-11-21
* Moved publisher out of internal AO repository into atomicobject.rb project.
View
2  README.txt
@@ -6,7 +6,7 @@ publisher
publisher is a module for extending a class with event subscription and firing capabilities. This is helpful for implementing objects that participate in the Observer design pattern.
-== FEATURES/PROBLEMS:
+== FEATURES:
* Nice syntax for declaring events that can be subscribed for
* Convenient event firing syntax
View
18 lib/publisher.rb
@@ -1,6 +1,6 @@
# See README.txt for synopsis
module Publisher
- VERSION = "1.1.0" #:nodoc:#
+ VERSION = "1.1.1" #:nodoc:#
# Use this method (or one of the aliases) to declare which events you support
# Once invoked, your class will have the neccessary supporting methods for subscribing and firing.
@@ -13,6 +13,19 @@ def has_events(*args)
alias :has_event :has_events
alias :can_fire :has_events
+ def published_events
+ return @published_events if @published_events == :any_event_is_ok
+ my_events = @published_events || []
+ if self.superclass.respond_to?(:published_events)
+ inherited = self.superclass.published_events
+ if inherited == :any_event_is_ok
+ return :any_event_is_ok
+ end
+ my_events += self.superclass.published_events
+ end
+ my_events
+ end
+
# Use this method to allow subscription and firing of arbitrary events.
# This is convenient if, eg, your class has dynamic event names.
# Don't use this unless you have to; it's better to declare your events if you
@@ -65,7 +78,8 @@ def fire(event, *args) #:nod
# Does nothing if the current class supports the named event.
# Raises RuntimeError otherwise.
def ensure_valid(event) #:nodoc:#
- events = self.class.class_eval { @published_events }
+# events = self.class.class_eval { @published_events }
+ events = self.class.published_events
return if events == :any_event_is_ok
raise "Event '#{event}' not available" unless events and events.include?(event)
end
View
98 test/publisher_test.rb
@@ -188,20 +188,6 @@ def test_parameter_mismatch_between_event_and_handler
assert_equal 'huh', out
end
-# Cannot inherit events right now
-# class SomebodyKid < Somebody
-# end
-#
-# def test_inheriting_events
-# obj = SomebodyKid.new
-# out = nil
-# obj.on :eat_this do |food|
-# out = food
-# end
-# obj.go "taco"
-# assert_equal "taco", out
-# end
-
def test_extending_publisher_doesnt_affect_normal_inheritance
obj = Billy.new('wheel')
assert_equal 'wheel', obj.chair
@@ -248,6 +234,56 @@ def test_multiple_subscriptions_for_same_event
assert_equal "burger", out1, "First subscription no go"
assert_equal "burger", out2, "Second subscription no go"
end
+
+ def test_subclasses_inherit_events
+ a = []
+ grampa = Grandfather.new
+ grampa.on :cannons do
+ a << "grampa's cannons"
+ end
+
+ dad = Dad.new
+ dad.on :cannons do
+ a << "dad's cannons"
+ end
+
+ dave = Dave.new
+ dave.on :cannons do
+ a << "dave's cannons"
+ end
+ dave.on :specific do
+ a << "dave's specific"
+ end
+
+ grampa.go
+ dad.go
+ dave.go
+ dave.doit
+
+ assert_equal [
+ "grampa's cannons",
+ "dad's cannons",
+ "dave's cannons",
+ "dave's specific"
+ ], a
+ end
+
+ def test_subclasses_inherit_wide_open_events
+ a = []
+ dynamo = Dynamo.new
+ dynamo.when :surprise do
+ a << "surprise"
+ end
+ dynamo.go(:surprise)
+
+ son = SonOfDynamo.new
+ son.when :hooray do
+ a << "hooray"
+ end
+ son.kick_it(:hooray)
+
+ assert_equal [ "surprise", "hooray" ], a
+ end
#
# HELPERS
@@ -355,4 +391,38 @@ def go(arg)
end
end
+ class Grandfather
+ extend Publisher
+ can_fire :cannons
+
+ def go
+ fire :cannons
+ end
+ end
+
+ class Dad < Grandfather
+ end
+
+ class Dave < Dad
+ can_fire :specific
+
+ def doit
+ fire :specific
+ end
+ end
+
+ class Dynamo
+ extend Publisher
+ can_fire_anything
+ def go(sym)
+ fire sym
+ end
+ end
+
+ class SonOfDynamo < Dynamo
+ def kick_it(sym)
+ fire sym
+ end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.