class CompositeEndpoint
A composite endpoint is a collection of endpoints that are used in order.
Definitions
def initialize(endpoints, **options)
Initialize a new composite endpoint.
Signature
-
parameter
endpointsArray(Generic) The endpoints to compose.
-
parameter
optionsHash Additional options to pass to the parent class and propagate to endpoints.
Implementation
def initialize(endpoints, **options)
super(**options)
# If any options were provided, propagate them to the endpoints:
if options.any?
endpoints = endpoints.map{|endpoint| endpoint.with(**options)}
end
@endpoints = endpoints
end
def to_s
Get a string representation of the composite endpoint.
Signature
-
returns
String A string representation listing all endpoints.
Implementation
def to_s
"composite:#{@endpoints.join(",")}"
end
def inspect
Get a detailed string representation of the composite endpoint.
Signature
-
returns
String A detailed string representation including all endpoints.
Implementation
def inspect
"\#<#{self.class} endpoints=#{@endpoints}>"
end
def with(**options)
Create a new composite endpoint with merged options.
Signature
-
parameter
optionsHash Additional options to merge with existing options.
-
returns
CompositeEndpoint A new composite endpoint instance with merged options.
Implementation
def with(**options)
self.class.new(endpoints.map{|endpoint| endpoint.with(**options)}, **@options.merge(options))
end
attr :endpoints
Signature
-
attribute
Array(Generic) The endpoints in this composite endpoint.
def size
The number of endpoints in the composite endpoint.
Implementation
def size
@endpoints.size
end
def each(&block)
Enumerate all endpoints in the composite endpoint.
Signature
-
yields
{|endpoint| ...} For each endpoint in the composite, yields it.
-
parameter
endpointGeneric An endpoint in the composite.
-
parameter
Implementation
def each(&block)
@endpoints.each do |endpoint|
endpoint.each(&block)
end
end
def connect(wrapper = self.wrapper, &block)
Connect to the first endpoint that succeeds.
Signature
-
parameter
wrapperWrapper The wrapper to use for connecting.
-
yields
{|socket| ...} If a block is given, yields the connected socket from the first successful endpoint.
-
parameter
socketSocket The connected socket.
-
parameter
-
returns
Socket The connected socket.
-
raises
Exception If all endpoints fail to connect, raises the last error encountered.
Implementation
def connect(wrapper = self.wrapper, &block)
last_error = nil
@endpoints.each do |endpoint|
begin
return endpoint.connect(wrapper, &block)
rescue => last_error
end
end
raise last_error
end
def bind(wrapper = self.wrapper, &block)
Bind all endpoints in the composite.
Signature
-
parameter
wrapperWrapper The wrapper to use for binding.
-
yields
{|socket| ...} For each endpoint that is bound, yields the bound socket.
-
parameter
socketSocket A bound socket.
-
parameter
-
returns
Array(Socket) An array of bound sockets if no block is given.
Implementation
def bind(wrapper = self.wrapper, &block)
if block_given?
@endpoints.each do |endpoint|
endpoint.bind(&block)
end
else
@endpoints.map(&:bind).flatten.compact
end
end