summaryrefslogtreecommitdiff
path: root/lib/nickserver/handler_chain.rb
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2016-09-19 09:51:38 +0200
committerAzul <azul@riseup.net>2016-09-19 09:51:38 +0200
commita1c7d68b05f142322a190b450971d27c076310a9 (patch)
tree75b171436d9f1109025904fda4cc650b2570a74b /lib/nickserver/handler_chain.rb
parentd3c9ec2c815a4cd79cfaf40d1de0365fdec90cb2 (diff)
refactor: separate handler chain from dispatcher
Handler Chain is 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). Extracted from the dispatcher so we can also handle exceptions there in the future. (So that if one of the network connections to the request_handlers fails we can continue while still tracking the failed exception.)
Diffstat (limited to 'lib/nickserver/handler_chain.rb')
-rw-r--r--lib/nickserver/handler_chain.rb26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/nickserver/handler_chain.rb b/lib/nickserver/handler_chain.rb
new file mode 100644
index 0000000..a0eba4d
--- /dev/null
+++ b/lib/nickserver/handler_chain.rb
@@ -0,0 +1,26 @@
+#
+# 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).
+#
+# Extracted from the dispatcher so we can also handle exceptions here in the
+# future.
+#
+
+module Nickserver
+ class HandlerChain
+
+ def initialize(*handlers)
+ @handlers = handlers
+ end
+
+ def handle(*args)
+ result = nil
+ _handled_by = @handlers.find{|h| result = h.call(*args)}
+ result
+ end
+
+ end
+end