Sha256: 93f1550f5e57c7525d584c7477469fbd6f3400c9882e936cdcbc7d3837688120

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

require 'erb'
require 'yaml'

class Ridgepole::Config
  class << self
    def load(config, env = 'development')
      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.password, 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,
        'database' => uri.path.sub(%r|\A/|, ''),
      }
     end
   end # of class methods
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ridgepole-0.6.6 lib/ridgepole/cli/config.rb
ridgepole-0.6.6.beta3 lib/ridgepole/cli/config.rb
ridgepole-0.7.0.alpha lib/ridgepole/cli/config.rb
ridgepole-0.6.6.beta2 lib/ridgepole/cli/config.rb
ridgepole-0.6.6.beta lib/ridgepole/cli/config.rb