diff options
author | azul <azul@riseup.net> | 2016-09-23 06:45:18 +0000 |
---|---|---|
committer | azul <azul@riseup.net> | 2016-09-23 06:45:18 +0000 |
commit | 6721f0732facd87404eecc288357fd1bd0de48cf (patch) | |
tree | 58b0f80b545987d5fc7f3dfdd4a3c1563cbc216e /lib/nickserver/handler_chain.rb | |
parent | e2aedcaade71dfe9103fdc8e705f59ece5f3a4d0 (diff) | |
parent | 68ffe9928620d3e5e3b96152ed4d37da90f6a89b (diff) |
Merge branch 'feature/deal-with-network-failures' into 'master'
Feature/deal with network failures
Also activates the new nicknym lookup.
See merge request !5
Diffstat (limited to 'lib/nickserver/handler_chain.rb')
-rw-r--r-- | lib/nickserver/handler_chain.rb | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/nickserver/handler_chain.rb b/lib/nickserver/handler_chain.rb new file mode 100644 index 0000000..afc24a5 --- /dev/null +++ b/lib/nickserver/handler_chain.rb @@ -0,0 +1,48 @@ +# +# Handler Chain +# +# A chain of handlers that respond to call. Invoking handle(*args) on the chain +# will call the handlers with the given args until one of them returns a result +# that is truethy (i.e. not false or nil). +# +# You can specify exception classes to rescue with +# handler_chain.continue_on ErrorClass1, ErrorClass2 +# These exceptions will be rescued and tracked. The chain will proceed even if +# one handler raised the given exception. Afterwards you can inspect them with +# handler_chain.rescued_exceptions +# + +module Nickserver + class HandlerChain + + def initialize(*handlers) + @handlers = handlers + @exceptions_to_rescue = [] + @rescued_exceptions = [] + end + + def continue_on(*exceptions) + self.exceptions_to_rescue += exceptions + end + + def handle(*args) + result = nil + _handled_by = @handlers.find{|h| result = try_handler(h, *args)} + result + end + + attr_reader :rescued_exceptions + + protected + + attr_writer :rescued_exceptions + attr_accessor :exceptions_to_rescue + + def try_handler(handler, *args) + result = handler.call(*args) + rescue *exceptions_to_rescue + self.rescued_exceptions << $! + result = false + end + end +end |