Permalink
Browse files

Allow delegating multiple handlers and remove dependency on Punchbloc…

…k::Connection#register_handlers
  • Loading branch information...
1 parent 09f36fd commit 80f6c0ffb613bb724e1266c0aac3fb841b83032a @bklang bklang committed Feb 14, 2012
Showing with 50 additions and 13 deletions.
  1. +8 −4 lib/adhearsion/xmpp.rb
  2. +3 −1 lib/adhearsion/xmpp/plugin.rb
  3. +39 −8 spec/adhearsion/xmpp_spec.rb
@@ -16,10 +16,14 @@ class XMPP
class << self
def register_handlers(&block)
- self.handlers = block
- unless plugin.nil?
- # Plugin has already initialized. Add these handlers directly.
- plugin.connection.register_handlers &block unless block.nil?
+ raise ArgumentError, "You must supply a block" unless block_given?
+ self.handlers ||= ThreadSafeArray.new
+ handlers << block
+ handlers.each do |handler|
+ unless plugin.nil?
+ # Plugin has already initialized. Add these handlers directly.
+ plugin.connection.instance_eval &handler
+ end
end
end
@@ -14,7 +14,9 @@ class Plugin < Adhearsion::Plugin
# Include the XMPP service in plugins initialization process
init :xmpp, :after => :punchblock do
Adhearsion::XMPP.plugin = Adhearsion::XMPP::Plugin.new
- connection.register_handlers &Adhearsion::XMPP.handlers unless Adhearsion::XMPP.handlers.nil?
+ Adhearsion::XMPP.handlers.each do |handler|
+ connection.instance_eval &handler
+ end unless Adhearsion::XMPP.handlers.nil?
end
run :xmpp do
@@ -5,7 +5,8 @@
subject { Adhearsion::XMPP }
before :each do
- Adhearsion::XMPP.plugin = nil
+ Adhearsion::XMPP.plugin = nil
+ Adhearsion::XMPP.handlers = nil
end
it 'should delegate methods to the plugin connection' do
@@ -17,22 +18,52 @@
Adhearsion::XMPP.foo
end
- it 'should hold on to handlers until the plugin is initialized' do
- mock_handlers = lambda { :does_not_matter }
+ it 'should hold on to a handler before the plugin is initialized' do
+ mock_handler = lambda { :does_not_matter }
Adhearsion::PunchblockPlugin.should_not_receive(:connection)
- Adhearsion::XMPP.register_handlers &mock_handlers
- Adhearsion::XMPP.handlers.should == mock_handlers
+ Adhearsion::XMPP.register_handlers &mock_handler
+ Adhearsion::XMPP.handlers.pop.should be mock_handler
+ end
+
+ it 'should hold on to multiple handlers' do
+ handler1 = lambda { :does_not_matter }
+ handler2 = lambda { :still_does_not_matter }
+
+ Adhearsion::XMPP.register_handlers &handler1
+ Adhearsion::XMPP.register_handlers &handler2
+ Adhearsion::XMPP.handlers.shift.should == handler1
+ Adhearsion::XMPP.handlers.shift.should == handler2
end
it 'should immediately register the handlers if the plugin is initialized' do
- mock_handlers = lambda { :does_not_matter }
+ mock_handler = lambda { :does_not_matter }
Adhearsion::XMPP.plugin = mock :plugin
connection = mock :connection
Adhearsion::XMPP.plugin.should_receive(:connection).and_return connection
- connection.should_receive(:register_handlers).once.with(&mock_handlers)
+ connection.should_receive(:instance_eval).once.with(&mock_handler)
+
+ Adhearsion::XMPP.register_handlers &mock_handler
+ end
+
+ it 'should register all stored handlers' do
+ handler1 = lambda { :does_not_matter }
+ handler2 = lambda { :still_does_not_matter }
+
+ Adhearsion::XMPP.register_handlers &handler1
+
+
+ Adhearsion::XMPP.plugin = mock :plugin
+ connection = mock :connection
+ Adhearsion::XMPP.plugin.should_receive(:connection).twice.and_return connection
+ connection.should_receive(:instance_eval).once.with(&handler1)
+ connection.should_receive(:instance_eval).once.with(&handler2)
+
+ Adhearsion::XMPP.register_handlers &handler2
+ end
- Adhearsion::XMPP.register_handlers &mock_handlers
+ it 'should raise ArgumentError if called without a block' do
+ expect { Adhearsion::XMPP.register_handlers }.to raise_error ArgumentError
end
end

0 comments on commit 80f6c0f

Please sign in to comment.