Async::ServiceSourceAsyncServiceConfiguration

class Configuration

Manages environments which describes how to host a specific set of services.

Environments are key-value maps with lazy value resolution. An environment can inherit from a parent environment, which can provide defaults

Definitions

def self.build(root: Dir.pwd, &block)

Build a configuration using a block.

Signature

parameter root String

The root directory for loading files.

yields {|loader| ...}

A loader instance for configuration.

returns Configuration

A new configuration instance.

Implementation

def self.build(root: Dir.pwd, &block)
	configuration = self.new
	
	loader = Loader.new(configuration, root)
	loader.instance_eval(&block)
	
	return configuration
end

def self.load(paths = ARGV)

Load configuration from file paths.

Signature

parameter paths Array(String)

File paths to load, defaults to ARGV.

returns Configuration

A new configuration instance.

Implementation

def self.load(paths = ARGV)
	configuration = self.new
	
	paths.each do |path|
		configuration.load_file(path)
	end
	
	return configuration
end

def self.for(*environments)

Create configuration from environments.

Signature

parameter environments Array

Environment instances.

returns Configuration

A new configuration instance.

Implementation

def self.for(*environments)
	self.new(environments)
end

def initialize(environments = [])

Initialize an empty configuration.

Implementation

def initialize(environments = [])
	@environments = environments
end

def empty?

Check if the configuration is empty.

Signature

returns Boolean

True if no environments are configured.

Implementation

def empty?
	@environments.empty?
end

def services(implementing: nil)

Enumerate all services in the configuration.

A service is an environment that has a service_class key.

Signature

parameter implementing Module

If specified, only services implementing this module will be returned/yielded.

yields {|service| ...}

Each service in the configuration.

Implementation

def services(implementing: nil)
	return to_enum(:services, implementing: implementing) unless block_given?
	
	@environments.each do |environment|
		if implementing.nil? or environment.implements?(implementing)
			if service = Generic.wrap(environment)
				yield service
			end
		end
	end
end

def controller(**options)

Create a controller for the configured services.

Signature

returns Controller

A controller that can be used to start/stop services.

Implementation

def controller(**options)
	Controller.new(self.services(**options).to_a)
end

def add(environment)

Add the environment to the configuration.

Implementation

def add(environment)
	@environments << environment
end

def load_file(path)

Load the specified configuration file. See Async::Service::Loader#load_file for more details.

Implementation

def load_file(path)
	Loader.load_file(self, path)
end