Class: Familia::Connection::ResponsibilityChain

Inherits:
Object
  • Object
show all
Defined in:
lib/familia/connection/handlers.rb

Overview

Manages ordered chain of connection handlers

NOTE: It is important that the last handler in a responsibility chain either always provides a connection or raises an error. Otherwise the end result will simply be nil without any guidance to the caller.

Instance Method Summary collapse

Constructor Details

#initializeResponsibilityChain

Returns a new instance of ResponsibilityChain.



18
19
20
# File 'lib/familia/connection/handlers.rb', line 18

def initialize
  @handlers = []
end

Instance Method Details

#add_handler(handler) ⇒ Object



22
23
24
25
# File 'lib/familia/connection/handlers.rb', line 22

def add_handler(handler)
  @handlers << handler
  self
end

#handle(uri) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/familia/connection/handlers.rb', line 27

def handle(uri)
  @handlers.each do |handler|
    connection = handler.handle(uri)
    if connection
      Fiber[:familia_connection_handler_class] = handler.class
      return connection
    end
  end

  # If we get here, no handler provided a connection
  nil
end