class ActiveRecord::DatabaseConfigurations::ConnectionUrlResolver

:nodoc:
Expands a connection string into a hash.

def database_from_path

Returns name of the database.
def database_from_path
  if @adapter == "sqlite3"
    # 'sqlite3:/foo' is absolute, because that makes sense. The
    # corresponding relative version, 'sqlite3:foo', is handled
    # elsewhere, as an "opaque".
    uri.path
  else
    # Only SQLite uses a filename as the "database" name; for
    # anything else, a leading slash would be silly.
    uri.path.delete_prefix("/")
  end
end

def initialize(url)

}
timeout: "3000"
pool: "5",
password: "bar",
username: "foo",
database: "foo_test",
port: 9000,
host: "localhost",
adapter: "postgresql",
# => {
ConnectionUrlResolver.new(url).to_hash
url = "postgresql://foo:bar@localhost:9000/foo_test?pool=5&timeout=3000"

== Example
:nodoc:
Expands a connection string into a hash.
def initialize(url)
  raise "Database URL cannot be empty" if url.blank?
  @uri     = uri_parser.parse(url)
  @adapter = @uri.scheme && @uri.scheme.tr("-", "_")
  @adapter = "postgresql" if @adapter == "postgres"
  if @uri.opaque
    @uri.opaque, @query = @uri.opaque.split("?", 2)
  else
    @query = @uri.query
  end
end

def query_hash

# => {}
"localhost"

returns empty hash if no query present.

# => { pool: "5", reaping_frequency: "2" }
"localhost?pool=5&reaping_frequency=2"

Converts the query parameters of the URI into a hash.
def query_hash
  Hash[(@query || "").split("&").map { |pair| pair.split("=", 2) }].symbolize_keys
end

def raw_config

def raw_config
  if uri.opaque
    query_hash.merge(
      adapter: @adapter,
      database: uri.opaque
    )
  else
    query_hash.reverse_merge(
      adapter: @adapter,
      username: uri.user,
      password: uri.password,
      port: uri.port,
      database: database_from_path,
      host: uri.hostname
    )
  end
end

def to_hash

Converts the given URL to a full connection hash.
def to_hash
  config = raw_config.compact_blank
  config.map { |key, value| config[key] = uri_parser.unescape(value) if value.is_a? String }
  config
end

def uri_parser

def uri_parser
  @uri_parser ||= URI::Parser.new
end