Class: Falcon::Configuration::Loader
- Inherits:
-
Object
- Object
- Falcon::Configuration::Loader
- Defined in:
- lib/falcon/configuration.rb
Overview
The domain specific language for loading configuration files.
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
The attached configuration instance.
-
#root ⇒ Object
readonly
The file-system root path which is injected into the environments as required.
Class Method Summary collapse
-
.load_file(configuration, path) ⇒ Object
Load the specified file into the given configuration.
Instance Method Summary collapse
-
#environment(name, *parents, &block) ⇒ Object
Add the named environment, with zero or more parent environments, defined using the specified ‘block`.
-
#host(name, *parents, &block) ⇒ Object
Define a host with the specified name.
-
#initialize(configuration, root = nil) ⇒ Loader
constructor
Initialize the loader, attached to a specific configuration instance.
-
#load(*features) ⇒ Object
Load specific features into the current configuration.
-
#proxy(name, *parents, &block) ⇒ Object
Define a proxy with the specified name.
-
#rack(name, *parents, &block) ⇒ Object
Define a rack application with the specified name.
-
#supervisor(&block) ⇒ Object
Define a supervisor instance Adds ‘root` key.
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.
78 79 80 81 82 83 |
# File 'lib/falcon/configuration.rb', line 78 def initialize(configuration, root = nil) @loaded = {} @configuration = configuration @environments = {} @root = root end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
The attached configuration instance.
91 92 93 |
# File 'lib/falcon/configuration.rb', line 91 def configuration @configuration end |
#root ⇒ Object (readonly)
The file-system root path which is injected into the environments as required.
87 88 89 |
# File 'lib/falcon/configuration.rb', line 87 def root @root end |
Class Method Details
.load_file(configuration, path) ⇒ Object
Load the specified file into the given configuration.
96 97 98 99 100 101 102 103 |
# File 'lib/falcon/configuration.rb', line 96 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`.
135 136 137 138 |
# File 'lib/falcon/configuration.rb', line 135 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.
143 144 145 146 147 148 149 150 |
# File 'lib/falcon/configuration.rb', line 143 def host(name, *parents, &block) environment = merge(name, *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.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/falcon/configuration.rb', line 110 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) ⇒ Object
Define a proxy with the specified name. Adds ‘root` and `authority` keys.
155 156 157 158 159 160 161 162 |
# File 'lib/falcon/configuration.rb', line 155 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.
167 168 169 170 171 172 173 174 |
# File 'lib/falcon/configuration.rb', line 167 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.
178 179 180 181 182 183 184 185 |
# File 'lib/falcon/configuration.rb', line 178 def supervisor(&block) name = File.join(@root, "supervisor") environment = merge(name, :supervisor, &block) environment[:root] = @root @configuration.add(environment.flatten) end |