Permalink
Browse files

added Proc / block support for event handlers

  • Loading branch information...
1 parent 60a9e91 commit d8cf6efcde7f7fe08d95ef9e455630fd61bedfaa @augusto committed Sep 17, 2011
Showing with 93 additions and 6 deletions.
  1. +2 −1 Gemfile
  2. +1 −0 lib/eventorz.rb
  3. +10 −3 lib/eventorz/extension/kernel.rb
  4. +24 −0 lib/eventorz/proc_handler.rb
  5. +43 −2 spec/eventorz_spec.rb
  6. +13 −0 spec/spec_helper.rb
View
@@ -10,6 +10,7 @@ group :development do
gem "bundler", "~> 1.0.0"
gem "jeweler", "~> 1.6.4"
gem "rcov", ">= 0"
- gem 'simplecov', ">= 0"
+ gem 'simplecov', ">= 0"
+ gem 'ruby-debug19'
end
View
@@ -1,4 +1,5 @@
require 'eventorz/event'
require 'eventorz/event_handler'
+require 'eventorz/proc_handler'
require 'eventorz/extension/module'
require 'eventorz/extension/kernel'
@@ -7,11 +7,18 @@ module Kernel
# object.event_name += handle instance, :method_name
#
private
- def handle( *args )
- if args.length == 1
+ def handle( *args, &block)
+ case
+ when (args.length == 0 and block_given?)
+ Eventorz::ProcHandler.new block
+ when (args.length == 1 and args[0].kind_of? Proc)
+ Eventorz::ProcHandler.new args[0]
+ when (args.length == 1 and args[0].kind_of? Symbol)
Eventorz::EventHandler.new self, args[0]
- elsif args.length == 2
+ when (args.length == 2 and args[1].kind_of? Symbol)
Eventorz::EventHandler.new args[0], args[1]
+ else
+ raise ArgumentError.new "usage: handle [:method | target, :method | &block | {block}]"
end
end
end
@@ -0,0 +1,24 @@
+module Eventorz
+ class ProcHandler
+ def initialize(proc)
+ @proc = proc
+ end
+
+ def fire(source, parameters)
+ @proc.call source, parameters
+ end
+
+ def ==(other)
+ return false unless other.respond_to?(:proc)
+ self.proc == other.proc
+ end
+
+ protected
+ attr_accessor :proc
+
+ public
+ def to_s
+ "#{self.class}:proc=#{self.proc}"
+ end
+ end
+end
View
@@ -36,7 +36,7 @@ def fire_event(message)
describe "can append event with obj.event += handler" do
- before :all do
+ before :each do
def test_handler
puts "handling event"
end
@@ -55,8 +55,35 @@ def test_handler
@instance.event_name.should contain_event_handler(self, :test_handler)
end
+
+ it "with a lambda" do
+ proc = lambda { |source, parameters| nil }
+ @instance.event_name += handle( proc )
+
+ @instance.event_name.should contain_proc_event_handler(proc)
+ end
+
+ it "with a proc" do
+ proc = Proc.new { |source, parameters| nil }
+ @instance.event_name += handle( proc )
+
+ @instance.event_name.should contain_proc_event_handler(proc)
+ end
+
+ it "with a block" do
+ @instance.event_name.send(:handlers).size.should == 0 #precondition
+
+ @instance.event_name += handle { nil }
+
+ @instance.event_name.send(:handlers).size.should == 1
+ end
end
+
+ it "raises error if handler parameters are incorrect" do
+ clazz = Class.new
+ lambda { clazz.send :handle, "string"}.should raise_exception(ArgumentError)
+ end
describe "invokes handlers" do
class TestHandler
@@ -83,7 +110,7 @@ def myHandler(source, parameters)
test_handler.events.should eql( [[instance, {:message => :event}]] )
end
-
+
it "in order" do
collector = []
test_handler_one = TestHandler.new collector
@@ -96,5 +123,19 @@ def myHandler(source, parameters)
collector.should eql( [ test_handler_one, test_handler_two ] )
end
+
+ it "in order (even proc, block and method handlers)" do
+ @collector = []
+ test_handler_one = TestHandler.new @collector
+ test_handler_two = Proc.new { |source,params| @collector << "proc" }
+
+ instance = TestSourceClass.new
+ instance.event_name += handle(test_handler_one, :myHandler)
+ instance.event_name += handle(test_handler_two)
+ instance.event_name += handle { @collector << "block"}
+ instance.fire_event :event
+
+ @collector.should eql( [ test_handler_one, "proc" , "block"] )
+ end
end
end
View
@@ -5,6 +5,8 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
require 'eventorz'
+require 'ruby-debug'; Debugger.start
+
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
@@ -25,6 +27,17 @@
end
end
+RSpec::Matchers.define :contain_proc_event_handler do |proc|
+ match do |event_handler|
+ handlers = event_handler.instance_variable_get("@handlers");
+ handlers.include? Eventorz::ProcHandler.new(proc)
+ end
+
+ failure_message_for_should do |event_handler|
+ "the event handler didn't include the handle for the proc #{proc}"
+ end
+end
+
RSpec::Matchers.define :contain_event_handler do |instance, method_name|
match do |event_handler|
handlers = event_handler.instance_variable_get("@handlers");

0 comments on commit d8cf6ef

Please sign in to comment.