module Sinatra::ConfigFile
def self.registered(base)
When the extension is registered sets the +environments+ setting to the
def self.registered(base) base.set :environments, %w[test production development] end
def config_file(*paths)
arguments, filtering the settings for the current environment. Note that
Loads the configuration from the YAML files whose +paths+ are passed as
def config_file(*paths) Dir.chdir(root || '.') do paths.each do |pattern| Dir.glob(pattern) do |file| raise UnsupportedConfigType unless ['.yml', '.yaml', '.erb'].include?(File.extname(file)) logger.info "loading config file '#{file}'" if logging? && respond_to?(:logger) document = ERB.new(File.read(file)).result yaml = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(document) : YAML.load(document) config = config_for_env(yaml) config.each_pair { |key, value| set(key, value) } end end end end
def config_for_env(hash)
settings applicable to the current environment. Note: It gives
Given a +hash+ containing application configuration it returns
def config_for_env(hash) return from_environment_key(hash) if environment_keys?(hash) hash.each_with_object(IndifferentHash[]) do |(k, v), acc| if environment_keys?(v) acc.merge!(k => v[environment.to_s]) if v.key?(environment.to_s) else acc.merge!(k => v) end end end
def environment_keys?(hash)
Returns true if supplied with a hash that has any recognized
def environment_keys?(hash) hash.is_a?(Hash) && hash.any? { |k, _| environments.include?(k.to_s) } end
def from_environment_key(hash)
Given a +hash+ returns the settings corresponding to the current
def from_environment_key(hash) hash[environment.to_s] || hash[environment.to_sym] || {} end