class Falcon::Configuration::Loader

The domain specific language for loading configuration files.

Attributes

configuration[R]

The attached configuration instance. @attribute [Configuration]

root[R]

The file-system root path which is injected into the environments as required. @attribute [String]

Public Class Methods

load_file(configuration, path) click to toggle source

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
new(configuration, root = nil) click to toggle source

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

environment(name, *parents, &block) click to toggle source

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
host(name, *parents, &block) click to toggle source

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(*features) click to toggle source

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
proxy(name, *parents, &block) click to toggle source

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
rack(name, *parents, &block) click to toggle source

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
supervisor(&block) click to toggle source

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

merge(name, *parents, &block) click to toggle source

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