Skip to content

Commit

Permalink
allow for passing handler responsibility to the next handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sprsquish committed Jun 12, 2009
1 parent 55f9e44 commit 2c530c3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
21 changes: 11 additions & 10 deletions lib/blather/client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,17 @@ def handle_stanza(stanza)
end

def call_handler_for(type, stanza)
if @handlers[type]
@handlers[type].find do |guards, handler|
if guards.first.is_a?(String)
unless (result = stanza.find(*guards)).empty?
handler.call(stanza, result)
end
elsif !guarded?(guards, stanza)
handler.call(stanza)
end
end
return unless handler = @handlers[type]
handler.find do |guards, handler|
catch(:pass) { call_handler handler, guards, stanza }
end
end

def call_handler(handler, guards, stanza)
if guards.first.respond_to?(:to_str) && !(result = stanza.find(*guards)).empty?
handler.call(stanza, result)
elsif !guarded?(guards, stanza)
handler.call(stanza)
end
end

Expand Down
12 changes: 12 additions & 0 deletions lib/blather/client/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ def jid
client.jid
end

##
# Halt the handler chain
def halt
throw :halt
end

##
# Pass responsibility to the next handler
def pass
throw :pass
end

##
# Request items or info from an entity
# discover (items|info), [jid], [node] do |response|
Expand Down
33 changes: 29 additions & 4 deletions spec/blather/client/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,38 @@

it 'allows for breaking out of handlers' do
stanza = Blather::Stanza::Iq.new
response = mock()
response.expects(:call).times(1)
response = mock(:iq => nil)
@client.register_handler(:iq) do |_|
response.call
response.iq
throw :halt
response.call
response.fail
end
@client.receive_data stanza
end

it 'allows for passing to the next handler of the same type' do
stanza = Blather::Stanza::Iq.new
response = mock(:iq1 => nil, :iq2 => nil)
@client.register_handler(:iq) do |_|
response.iq1
throw :pass
response.fail
end
@client.register_handler(:iq) do |_|
response.iq2
end
@client.receive_data stanza
end

it 'allows for passing to the next handler in the heirarchy' do
stanza = Blather::Stanza::Iq::Query.new
response = mock(:query => nil, :iq => nil)
@client.register_handler(:query) do |_|
response.query
throw :pass
response.fail
end
@client.register_handler(:iq) { |_| response.iq }
@client.receive_data stanza
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/blather/client/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
@dsl.shutdown
end

it 'can throw a halt' do
catch(:halt) { @dsl.halt }
end

it 'can throw a pass' do
catch(:pass) { @dsl.pass }
end

it 'sets up handlers' do
type = :message
guards = [:chat?, {:body => 'exit'}]
Expand Down

0 comments on commit 2c530c3

Please sign in to comment.