Sha256: 4403bd53b1e6f6586a17339d9a7e803c0c3979a118925b8d40e5b41c2930b663

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023-2024, by Samuel Williams.

require_relative 'generic'

module IO::Endpoint
	# A composite endpoint is a collection of endpoints that are used in order.
	class CompositeEndpoint < Generic
		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
			"composite:#{@endpoints.join(",")}"
		end
		
		def inspect
			"\#<#{self.class} endpoints=#{@endpoints}>"
		end
		
		def with(**options)
			self.class.new(endpoints.map{|endpoint| endpoint.with(**options)}, **@options.merge(options))
		end
		
		attr :endpoints
		
		# The number of endpoints in the composite endpoint.
		def size
			@endpoints.size
		end
		
		def each(&block)
			@endpoints.each do |endpoint|
				endpoint.each(&block)
			end
		end
		
		def connect(wrapper = Wrapper.default, &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 = Wrapper.default, &block)
			if block_given?
				@endpoints.each do |endpoint|
					endpoint.bind(&block)
				end
			else
				@endpoints.map(&:bind).flatten.compact
			end
		end
	end
	
	def self.composite(*endpoints, **options)
		CompositeEndpoint.new(endpoints, **options)
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
io-endpoint-0.14.0 lib/io/endpoint/composite_endpoint.rb