Class: ActiveRecord::DatabaseConfigurations::ConnectionUrlResolver
Do not use. This class is for internal use only.
Relationships & Source Files | |
Inherits: | Object |
Defined in: | activerecord/lib/active_record/database_configurations/connection_url_resolver.rb |
Overview
Expands a connection string into a hash.
Class Method Summary
-
.new(url) ⇒ ConnectionUrlResolver
constructor
Example.
Instance Attribute Summary
- #uri readonly private
Instance Method Summary
-
#to_hash
Converts the given URL to a full connection hash.
-
#database_from_path
private
Returns name of the database.
-
#query_hash
private
Converts the query parameters of the URI into a hash.
- #raw_config private
- #resolved_adapter private
- #uri_parser private
Constructor Details
.new(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"
}
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 25
def initialize(url) raise "Database URL cannot be empty" if url.blank? @uri = uri_parser.parse(url) @adapter = resolved_adapter if @uri.opaque @uri.opaque, @query = @uri.opaque.split("?", 2) else @query = @uri.query end end
Instance Attribute Details
#uri (readonly, private)
[ GitHub ]# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 45
attr_reader :uri
Instance Method Details
#database_from_path (private)
Returns name of the database.
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 91
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
#query_hash (private)
Converts the query parameters of the URI into a hash.
"localhost?pool=5&reaping_frequency=2"
# => { pool: "5", reaping_frequency: "2" }
returns empty hash if no query present.
"localhost"
# => {}
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 60
def query_hash Hash[(@query || "").split("&").map { |pair| pair.split("=", 2) }].symbolize_keys end
#raw_config (private)
[ GitHub ]# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 64
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
#resolved_adapter (private)
[ GitHub ]# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 82
def resolved_adapter adapter = uri.scheme && @uri.scheme.tr("-", "_") if adapter && ActiveRecord.protocol_adapters[adapter] adapter = ActiveRecord.protocol_adapters[adapter] end adapter end
#to_hash
Converts the given URL to a full connection hash.
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 38
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
#uri_parser (private)
[ GitHub ]# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 47
def uri_parser @uri_parser ||= URI::RFC2396_Parser.new end