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 = [], container_policy: nil) ⇒ Configuration

Initialize an empty configuration.



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

def initialize(environments = [], container_policy: nil)
  @environments = environments
  @container_policy = container_policy
end

Instance Attribute Details

#container_policyObject

Returns the value of attribute container_policy.



63
64
65
# File 'lib/async/service/configuration.rb', line 63

def container_policy
  @container_policy
end

#environmentsObject (readonly)

Returns the value of attribute environments.



62
63
64
# File 'lib/async/service/configuration.rb', line 62

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
28
29
30
31
32
# File 'lib/async/service/configuration.rb', line 20

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

.for(*environments) ⇒ Object

Create configuration from environments.



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

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

.load(paths = ARGV) ⇒ Object

Load configuration from file paths.



37
38
39
40
41
42
43
44
45
# File 'lib/async/service/configuration.rb', line 37

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.



108
109
110
# File 'lib/async/service/configuration.rb', line 108

def add(environment)
  @environments << environment
end

#empty?Boolean

Check if the configuration is empty.

Returns:



67
68
69
# File 'lib/async/service/configuration.rb', line 67

def empty?
  @environments.empty?
end

#load_file(path) ⇒ Object

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



113
114
115
# File 'lib/async/service/configuration.rb', line 113

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

#make_controller(container_policy: @container_policy, implementing: nil, **options) ⇒ Object Also known as: controller

Create a controller for the configured services.



94
95
96
97
98
99
100
101
102
# File 'lib/async/service/configuration.rb', line 94

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

#services(implementing: nil) ⇒ Object

Enumerate all services in the configuration.

A service is an environment that has a service_class key.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/async/service/configuration.rb', line 77

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