summaryrefslogtreecommitdiff
path: root/lib/nickserver/handler_chain.rb
blob: a0eba4d0d86f5d72c2ab1bbe8217c53207cfdd90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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