Class: ActiveRecord::DatabaseConfigurations::ConnectionUrlResolver

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/database_configurations/connection_url_resolver.rb

Overview

Expands a connection string into a hash.

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ ConnectionUrlResolver

Example

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


23
24
25
26
27
28
29
30
31
32
33
34
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 23

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

Instance Method Details

#to_hashObject

Converts the given URL to a full connection hash.



37
38
39
40
41
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 37

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