Class: Pgchief::ConnectionString
- Inherits:
-
Object
- Object
- Pgchief::ConnectionString
- Defined in:
- lib/pgchief/connection_string.rb
Overview
Class to parse and manipulate connection strings
Constant Summary collapse
- URL_REGEX =
%r{(?x)\A postgres(ql)?:// (?<username>[^:@]*)? :?(?<password>[^@]*)? @?(?<host>[^:]*)? :?(?<port>\d+)? /?(?<database>[^?]*)? \z}
Instance Attribute Summary collapse
-
#database_url ⇒ Object
readonly
Returns the value of attribute database_url.
Instance Method Summary collapse
- #database ⇒ Object
- #host ⇒ Object
-
#initialize(database_url, username: nil, password: nil, host: nil, port: nil, database: nil) ⇒ ConnectionString
constructor
A new instance of ConnectionString.
- #password ⇒ Object
- #port ⇒ Object
- #to_s ⇒ Object
- #username ⇒ Object
Constructor Details
#initialize(database_url, username: nil, password: nil, host: nil, port: nil, database: nil) ⇒ ConnectionString
Returns a new instance of ConnectionString.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pgchief/connection_string.rb', line 17 def initialize( database_url, username: nil, password: nil, host: nil, port: nil, database: nil ) @database_url = database_url @host = host @username = username @password = password @port = port @database = database end |
Instance Attribute Details
#database_url ⇒ Object (readonly)
Returns the value of attribute database_url.
15 16 17 |
# File 'lib/pgchief/connection_string.rb', line 15 def database_url @database_url end |
Instance Method Details
#database ⇒ Object
53 54 55 |
# File 'lib/pgchief/connection_string.rb', line 53 def database @database || matched[:database] || '' end |
#host ⇒ Object
37 38 39 |
# File 'lib/pgchief/connection_string.rb', line 37 def host @host || (matched[:username] if matched[:host].empty?) || matched[:host] end |
#password ⇒ Object
45 46 47 |
# File 'lib/pgchief/connection_string.rb', line 45 def password @password || matched[:password] || '' end |
#port ⇒ Object
49 50 51 |
# File 'lib/pgchief/connection_string.rb', line 49 def port @port || matched[:port] || '5432' end |
#to_s ⇒ Object
33 34 35 |
# File 'lib/pgchief/connection_string.rb', line 33 def to_s "postgresql://#{username}:#{password}@#{host}:#{port}/#{database}" end |
#username ⇒ Object
41 42 43 |
# File 'lib/pgchief/connection_string.rb', line 41 def username @username || ('' if matched[:host].empty? && !matched[:username].empty?) || matched[:username] || '' end |