Permalink
Browse files

multiple handlers for the same event work

  • Loading branch information...
1 parent 63e4db8 commit fa04fa0c1293591b26b3e2ea1df630a934a2fdb2 @telent committed Apr 21, 2012
Showing with 25 additions and 5 deletions.
  1. +9 −5 lib/lizard/monitor.rb
  2. +16 −0 test/unit/monitor_test.rb
View
14 lib/lizard/monitor.rb
@@ -9,16 +9,20 @@ def name(value)
end
end
def listen(event,&blk)
- @clisteners||=Hash.new {|h,k| h[k]=[] }
- @clisteners[event] << blk
+ @clisteners||=[]
+ @clisteners << {event => blk}
end
end
attr_reader :listeners
def init_listeners(clss=self.class)
- @listeners ||= {}
- @listeners=@listeners.merge(clss.instance_variable_get(:@clisteners) ||{})
- if (sup=clss.superclass) != Object
+ @listeners ||= Hash.new {|h,k| h[k]=[] }
+ v=clss.instance_variable_get(:@clisteners)
+ v and v.each do |a|
+ k,v=a.first
+ @listeners[k] << v
+ end
+ if (sup=clss.superclass)
init_listeners sup
end
end
View
16 test/unit/monitor_test.rb
@@ -34,6 +34,22 @@
m2.listeners[:enable].map(&:call)
assert succeeded
end
+ it "inherited and direct handlers both work" do
+ succeeded=0
+ m=Class.new(Lizard::Monitor) do
+ listen :enable do
+ succeeded+=1
+ end
+ end
+ m2=Class.new(m) do
+ listen :enable do
+ succeeded+=1
+ end
+ end.new
+ assert_equal 2, m2.listeners[:enable].length
+ m2.listeners[:enable].map(&:call)
+ assert_equal 2, succeeded
+ end
end
end

0 comments on commit fa04fa0

Please sign in to comment.