Class: Falcon::Configuration::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/falcon/configuration.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.



94
95
96
97
98
99
# File 'lib/falcon/configuration.rb', line 94

def initialize(configuration, root = nil)
	@loaded = {}
	@configuration = configuration
	@environments = {}
	@root = root
end

Instance Attribute Details

#configurationObject (readonly)

The attached configuration instance.



107
108
109
# File 'lib/falcon/configuration.rb', line 107

def configuration
  @configuration
end

#rootObject (readonly)

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



103
104
105
# File 'lib/falcon/configuration.rb', line 103

def root
  @root
end

Class Method Details

.load_file(configuration, path) ⇒ Object

Load the specified file into the given configuration.



112
113
114
115
116
117
118
119
# File 'lib/falcon/configuration.rb', line 112

def self.load_file(configuration, path)
	path = File.realpath(path)
	root = File.dirname(path)
	
	loader = self.new(configuration, root)
	
	loader.instance_eval(File.read(path), path)
end

Instance Method Details

#environment(name, *parents, &block) ⇒ Object

Add the named environment, with zero or more parent environments, defined using the specified ‘block`.

Raises:

  • (KeyError)


142
143
144
145
# File 'lib/falcon/configuration.rb', line 142

def environment(name, *parents, &block)
	raise KeyError.new("#{name} is already set", key: name) if @environments.key?(name)
	@environments[name] = merge(name, *parents, &block)
end

#host(name, *parents, &block) ⇒ Object

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



150
151
152
153
154
155
156
157
# File 'lib/falcon/configuration.rb', line 150

def host(name, *parents, &block)
	environment = merge(name, :host, *parents, &block)
	
	environment[:root] = @root
	environment[:authority] = name
	
	@configuration.add(environment.flatten)
end

#load(*features) ⇒ Object

Load specific features into the current configuration.

Falcon provides default environments for different purposes. These are included in the gem, in the ‘environments/` directory. This method loads the code in those files into the current configuration.



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/falcon/configuration.rb', line 126

def load(*features)
	features.each do |feature|
		next if @loaded.include?(feature)
		
		relative_path = File.join(__dir__, "environments", "#{feature}.rb")
		
		self.instance_eval(File.read(relative_path), relative_path)
		
		@loaded[feature] = relative_path
	end
end

#proxy(name, *parents, &block) ⇒ Object

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



162
163
164
165
166
167
168
169
# File 'lib/falcon/configuration.rb', line 162

def proxy(name, *parents, &block)
	environment = merge(name, :proxy, *parents, &block)
	
	environment[:root] = @root
	environment[:authority] = name
	
	@configuration.add(environment.flatten)
end

#rack(name, *parents, &block) ⇒ Object

Define a rack application with the specified name. Adds ‘root` and `authority` keys.



174
175
176
177
178
179
180
181
# File 'lib/falcon/configuration.rb', line 174

def rack(name, *parents, &block)
	environment = merge(name, :rack, *parents, &block)
	
	environment[:root] = @root
	environment[:authority] = name
	
	@configuration.add(environment.flatten)
end

#supervisor(&block) ⇒ Object

Define a supervisor instance Adds ‘root` key.



185
186
187
188
189
190
191
192
# File 'lib/falcon/configuration.rb', line 185

def supervisor(&block)
	name = File.join(@root, "supervisor")
	environment = merge(name, :supervisor, &block)
	
	environment[:root] = @root
	
	@configuration.add(environment.flatten)
end