IO::EndpointSourceIOEndpointNamedEndpoints

class NamedEndpoints

A named endpoints collection is a hash of endpoints that can be accessed by name.

Unlike class IO::Endpoint::CompositeEndpoint, which treats endpoints as an ordered list for failover, NamedEndpoints allows you to access endpoints by symbolic names, making it useful for scenarios where you need to run the same application on multiple endpoints with different configurations (e.g., HTTP/1 and HTTP/2 on different ports).

Definitions

def initialize(endpoints)

Initialize a new named endpoints collection.

Signature

parameter endpoints Hash(Symbol, Generic)

A hash mapping endpoint names to endpoint instances.

Implementation

def initialize(endpoints)
	@endpoints = endpoints
end

def to_s

Get a string representation of the named endpoints.

Signature

returns String

A string representation listing all named endpoints.

Implementation

def to_s
	parts = @endpoints.map do |name, endpoint|
		"#{name}:#{endpoint}"
	end
	"named:#{parts.join(",")}"
end

def inspect

Get a detailed string representation of the named endpoints.

Signature

returns String

A detailed string representation including all named endpoints.

Implementation

def inspect
	parts = @endpoints.map do |name, endpoint|
		"#{name}: #{endpoint.inspect}"
	end
	"\#<#{self.class} #{parts.join(", ")}>"
end

attr :endpoints

Signature

attribute Hash(Symbol, Generic)

The endpoints hash mapping names to endpoint instances.

def [](key)

Access an endpoint by its name.

Signature

parameter key Symbol

The name of the endpoint to access.

returns Generic, nil

The endpoint with the given name, or nil if not found.

Implementation

def [] key
	@endpoints[key]
end

def each(&block)

Enumerate all endpoints with their names.

Signature

yields {|name, endpoint| ...}

For each endpoint, yields the name and endpoint.

parameter name Symbol

The name of the endpoint.

parameter endpoint Generic

The endpoint.

Implementation

def each(&block)
	@endpoints.each(&block)
end

def bound(**options)

Create a new named endpoints instance with all endpoints bound.

Signature

parameter options Hash

Options to pass to each endpoint's bound method.

returns NamedEndpoints

A new instance with bound endpoints.

Implementation

def bound(**options)
	self.class.new(
		@endpoints.transform_values{|endpoint| endpoint.bound(**options)}
	)
end

def connected(**options)

Create a new named endpoints instance with all endpoints connected.

Signature

parameter options Hash

Options to pass to each endpoint's connected method.

returns NamedEndpoints

A new instance with connected endpoints.

Implementation

def connected(**options)
	self.class.new(
		@endpoints.transform_values{|endpoint| endpoint.connected(**options)}
	)
end

def close

Close all endpoints in the collection. Calls close on each endpoint value.

Signature

returns void

Implementation

def close
	@endpoints.each_value(&:close)
end