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)
	
	if block.arity == 0
		loader.instance_eval(&block)
	else
		yield loader
	end
	
	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 = [], container_policy: nil)

Initialize an empty configuration.

Signature

parameter environments Array

Environment instances.

parameter container_policy Proc

Optional proc that returns a policy for container lifecycle management.

Implementation

def initialize(environments = [], container_policy: nil)
	@environments = environments
	@container_policy = container_policy
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 make_controller(container_policy: @container_policy, implementing: nil, **options)

Create a controller for the configured services.

Signature

parameter container_policy Proc

A proc that returns the policy to use for managing child lifecycle events.

parameter options Hash

Additional options passed to the controller.

returns Controller

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

Implementation

def make_controller(container_policy: @container_policy, implementing: nil, **options)
	controller = Controller.new(self.services(implementing: implementing).to_a, **options)
	
	if container_policy
		controller.define_singleton_method(:make_policy, &container_policy)
	end
	
	return controller
end

alias controller make_controller

Alias for backwards compatibility.

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