summaryrefslogtreecommitdiff
path: root/lib/nickserver/handler_chain.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/nickserver/handler_chain.rb')
-rw-r--r--lib/nickserver/handler_chain.rb48
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