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

Version Path
ridgepole-0.7.1.beta lib/ridgepole/cli/config.rb
ridgepole-0.7.0 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.rc1 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta22 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta21 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta20 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta19 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta18 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta17 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta16 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta15 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta14 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta13 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta12 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta11 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta10 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta9 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta8 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta7 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.beta6 lib/ridgepole/cli/config.rb