Sha256: 87cf5aa038ffb9f93f34852ba510fc6486a27eaa937465cd1e3be0ae4f8ee278
Contents?: true
Size: 1.27 KB
Versions: 25
Compression:
Stored size: 1.27 KB
Contents
require 'erb' require 'yaml' class Ridgepole::Config class << self def load(config, env = 'development') if config =~ /\Aenv:(.+)\z/ config = ENV.fetch($1) end if File.exist?(config) parsed_config = parse_config_file(config) elsif (expanded = File.expand_path(config)) and File.exist?(expanded) parsed_config = parse_config_file(expanded) else parsed_config = YAML.load(ERB.new(config).result) end unless parsed_config.kind_of?(Hash) parsed_config = parse_database_url(config) end if parsed_config.has_key?(env.to_s) parsed_config.fetch(env.to_s) else parsed_config end end private def parse_config_file(path) yaml = ERB.new(File.read(path)).result YAML.load(yaml) end def parse_database_url(config) uri = URI.parse(config) if [uri.scheme, uri.user, uri.host, uri.path].any? {|i| i.nil? } raise "Invalid config: #{config.inspect}" end { 'adapter' => uri.scheme, 'username' => uri.user, 'password' => uri.password, 'host' => uri.host, 'port' => uri.port, 'database' => uri.path.sub(%r|\A/|, ''), } end end # of class methods end
Version data entries
25 entries across 25 versions & 1 rubygems