Class: Async::Service::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/async/service/configuration.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environments = []) ⇒ Configuration

Initialize an empty configuration.



50
51
52
# File 'lib/async/service/configuration.rb', line 50

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

Instance Attribute Details

#environmentsObject (readonly)

Returns the value of attribute environments.



54
55
56
# File 'lib/async/service/configuration.rb', line 54

def environments
  @environments
end

Class Method Details

.build(root: Dir.pwd, &block) ⇒ Object

Build a configuration using a block.



20
21
22
23
24
25
26
27
# File 'lib/async/service/configuration.rb', line 20

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

.for(*environments) ⇒ Object

Create configuration from environments.



45
46
47
# File 'lib/async/service/configuration.rb', line 45

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

.load(paths = ARGV) ⇒ Object

Load configuration from file paths.



32
33
34
35
36
37
38
39
40
# File 'lib/async/service/configuration.rb', line 32

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

Instance Method Details

#add(environment) ⇒ Object

Add the environment to the configuration.



88
89
90
# File 'lib/async/service/configuration.rb', line 88

def add(environment)
	@environments << environment
end

#controller(**options) ⇒ Object

Create a controller for the configured services.



83
84
85
# File 'lib/async/service/configuration.rb', line 83

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

#empty?Boolean

Check if the configuration is empty.

Returns:

  • (Boolean)


58
59
60
# File 'lib/async/service/configuration.rb', line 58

def empty?
	@environments.empty?
end

#load_file(path) ⇒ Object

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



93
94
95
# File 'lib/async/service/configuration.rb', line 93

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

#services(implementing: nil) ⇒ Object

Enumerate all services in the configuration.

A service is an environment that has a ‘service_class` key.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/async/service/configuration.rb', line 68

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 = GenericService.wrap(environment)
				yield service
			end
		end
	end
end