Class: Async::Service::Loader

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

Overview

The domain specific language for loading configuration files.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, root = nil) ⇒ Loader

Initialize the loader, attached to a specific configuration instance. Any environments generated by the loader will be added to the configuration.



16
17
18
19
# File 'lib/async/service/loader.rb', line 16

def initialize(configuration, root = nil)
	@configuration = configuration
	@root = root
end

Instance Attribute Details

#rootObject (readonly)

The file-system root path which is injected into the environments as required.



23
24
25
# File 'lib/async/service/loader.rb', line 23

def root
  @root
end

Class Method Details

.load_file(configuration, path) ⇒ Object

Load the specified file into the given configuration.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/async/service/loader.rb', line 28

def self.load_file(configuration, path)
	realpath = ::File.realpath(path)
	root = ::File.dirname(realpath)
	
	loader = self.new(configuration, root)
	
	if ::Module.method_defined?(:set_temporary_name)
		loader.singleton_class.set_temporary_name("#{self}[#{path.inspect}]")
	end
	
	loader.instance_eval(File.read(path), path)
end

Instance Method Details

#environment(**initial, &block) ⇒ Object

Create an environment.



48
49
50
# File 'lib/async/service/loader.rb', line 48

def environment(**initial, &block)
	Environment.build(**initial, &block)
end

#load_file(path) ⇒ Object

Load a configuration file relative to the loader’s root path.



43
44
45
# File 'lib/async/service/loader.rb', line 43

def load_file(path)
	Loader.load_file(@configuration, File.expand_path(path, @root))
end

#service(name = nil, **options, &block) ⇒ Object

Define a service with the specified name. Adds ‘root` and `name` keys.



55
56
57
58
59
60
# File 'lib/async/service/loader.rb', line 55

def service(name = nil, **options, &block)
	options[:name] = name
	options[:root] ||= @root
	
	@configuration.add(self.environment(**options, &block))
end