class Falcon::Configuration::Loader
The domain specific language for loading configuration files.
Attributes
The attached configuration instance. @attribute [Configuration]
The file-system root path which is injected into the environments as required. @attribute [String]
Public Class Methods
Load the specified file into the given configuration. @parameter configuration [Configuration] @oaram path [String] The path to the configuration file, e.g. `falcon.rb`.
# 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
Initialize the loader, attached to a specific configuration instance. Any environments generated by the loader will be added to the configuration. @parameter configuration [Configuration] @parameter root [String] The file-system root path for relative path computations.
# File lib/falcon/configuration.rb, line 94 def initialize(configuration, root = nil) @loaded = {} @configuration = configuration @environments = {} @root = root end
Public Instance Methods
Add the named environment, with zero or more parent environments, defined using the specified `block`. @parameter name [String] The name of the environment. @parameter parents [Array(Symbol)] The names of the parent environments to inherit. @yields {…} The block that will generate the environment.
# File lib/falcon/configuration.rb, line 151 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
Define a host with the specified name. Adds `root` and `authority` keys. @parameter name [String] The name of the environment, usually a hostname.
# File lib/falcon/configuration.rb, line 159 def host(name, *parents, &block) environment = merge(name, *parents, &block) environment[:root] = @root environment[:authority] = name @configuration.add(environment.flatten) end
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.
@parameter features [Array(Symbol)] The features to load.
# File lib/falcon/configuration.rb, line 126 def load(*features) features.each do |feature| next if @loaded.include?(feature) case feature when Symbol relative_path = File.join(__dir__, "environments", "#{feature}.rb") self.instance_eval(File.read(relative_path), relative_path) @loaded[feature] = relative_path when Module feature.load(self) @loaded[feature] = feature else raise LoadError, "Unsure about how to load #{feature}!" end end end
Define a proxy with the specified name. Adds `root` and `authority` keys. @parameter name [String] The name of the environment, usually a hostname.
# File lib/falcon/configuration.rb, line 171 def proxy(name, *parents, &block) environment = merge(name, :proxy, *parents, &block) environment[:root] = @root environment[:authority] = name @configuration.add(environment.flatten) end
Define a rack application with the specified name. Adds `root` and `authority` keys. @parameter name [String] The name of the environment, usually a hostname.
# File lib/falcon/configuration.rb, line 183 def rack(name, *parents, &block) environment = merge(name, :rack, *parents, &block) environment[:root] = @root environment[:authority] = name @configuration.add(environment.flatten) end
Define a supervisor instance Adds `root` key.
# File lib/falcon/configuration.rb, line 194 def supervisor(&block) name = File.join(@root, "supervisor") environment = merge(name, :supervisor, &block) environment[:root] = @root @configuration.add(environment.flatten) end
Private Instance Methods
Build a new environment with the specified name and the given parents. @parameter name [String] @parameter parents [Array(Build::Environment)] @yields {…} The block that will generate the environment.
# File lib/falcon/configuration.rb, line 209 def merge(name, *parents, &block) environments = parents.map{|name| @environments.fetch(name)} parent = Build::Environment.combine(*environments) Build::Environment.new(parent, name: name, &block) end