class Slugforge::Configuration
Handles loading configuration data from files and the environment. Order of precedence:
1) ENV 2) `pwd`/.slugforge 3) $HOME/.slugforge 4) /etc/slugforge
We load in reverse order, allowing us to simply overwrite values whenever found.
Attributes
configuration_files[RW]
values[R]
Public Class Methods
new(options = {})
click to toggle source
# File lib/slugforge/configuration.rb, line 49 def initialize(options = {}) @slugin_manager = SluginManager.new self.load update_from_options options end
option(name, config)
click to toggle source
# File lib/slugforge/configuration.rb, line 27 def option(name, config) raise "configuration option #{name} has already been defined" if options.key?(name) options[name] = config define_method(name) { values[name] } end
options()
click to toggle source
# File lib/slugforge/configuration.rb, line 23 def options @options ||= {} end
Public Instance Methods
activate_slugins()
click to toggle source
# File lib/slugforge/configuration.rb, line 62 def activate_slugins @slugin_manager.activate_slugins(self) unless disable_slugins end
defaults()
click to toggle source
Get a hash of all options with default values. The list of values is initialized with the result.
# File lib/slugforge/configuration.rb, line 58 def defaults @values = Hash[self.class.options.select { |_, c| c.key?(:default) }.map { |n,c| [n, c[:default]] }].merge(@values) end
Protected Instance Methods
load()
click to toggle source
# File lib/slugforge/configuration.rb, line 67 def load # Read configuration files to load list of slugins. Load the slugin classes so that # their configuration options are added and reload the configs to populate the new # options. @values = {} load_configuration_files defaults locate_slugins #TODO: disable individual slugins via configuration load_slugins unless disable_slugins load_configuration_files read_env end
load_configuration_files()
click to toggle source
# File lib/slugforge/configuration.rb, line 83 def load_configuration_files self.class.configuration_files.each { |f| read_yaml f } end
read_env()
click to toggle source
Attempt to read option keys from the environment
# File lib/slugforge/configuration.rb, line 106 def read_env update_with { |config| config[:env] && ENV[config[:env]] } end
read_yaml(path)
click to toggle source
Attempt to read option keys from a YAML file
# File lib/slugforge/configuration.rb, line 88 def read_yaml(path) return unless File.exist?(path) source = YAML.load_file(path) return unless source.is_a?(Hash) update_with { |config| read_yaml_key(source, config[:key]) } end
read_yaml_key(source, key)
click to toggle source
Split a dot-separated key and locate the value from a hash loaded by YAML.
eg. `aws.bucket` looks for `source['aws']['bucket']`.
# File lib/slugforge/configuration.rb, line 98 def read_yaml_key(source, key) return unless key.is_a?(String) paths = key.split('.') source = source[paths.shift] until paths.empty? || source.nil? source end
update_from_options(options={})
click to toggle source
Update values with a hash of options.
# File lib/slugforge/configuration.rb, line 111 def update_from_options(options={}) update_with { |config| config[:option] && options[config[:option]] } end
update_with() { |config| ... }
click to toggle source
For every option we yield the configuration and expect a value back. If the block returns a value we set the option to it.
# File lib/slugforge/configuration.rb, line 117 def update_with(&blk) self.class.options.each do |name, config| value = yield(config) @values[name] = value unless value.nil? end end