class RailsDbConfigResolver::DatabaseConfig

Public Class Methods

from_url(url_string) click to toggle source
# File lib/rails_db_config_resolver/database_config.rb, line 9
def self.from_url(url_string)
  url = URI.parse(url_string)

  pool = (url.query.match(/pool=(\d+)/)[1] if url.query)
  database = url.path[1..-1]

  new(
    adapter: url.scheme,
    host: url.host,
    port: url.port,
    username: url.user,
    password: url.password,
    database: database,
    pool: pool
  )
end
new(*) click to toggle source
Calls superclass method
# File lib/rails_db_config_resolver/database_config.rb, line 30
def initialize(*)
  super

  @pool = cast_to_integer_or_nil(@pool)
end

Public Instance Methods

merge(other) click to toggle source
# File lib/rails_db_config_resolver/database_config.rb, line 52
def merge(other)
  my_hash = to_hash
  other_hash = other.to_hash.reject { |_, value| value.nil? }
  self.class.from_hash(my_hash.merge(other_hash))
end
to_hash() click to toggle source
# File lib/rails_db_config_resolver/database_config.rb, line 36
def to_hash
  {
    adapter: @adapter,
    host: @host,
    port: @port,
    username: @username,
    password: @password,
    database: @database,
    pool: @pool
  }
end
to_url() click to toggle source
# File lib/rails_db_config_resolver/database_config.rb, line 48
def to_url
  URI::Generic.build(build_url_hash).to_s
end

Private Instance Methods

build_url_hash() click to toggle source
# File lib/rails_db_config_resolver/database_config.rb, line 60
def build_url_hash
  {
    scheme: @adapter,
    host: @host,
    port: @port,
    path: "/#{@database}"
  }.tap do |hash|
    hash[:query] = "pool=#{@pool}" if @pool
    hash[:userinfo] = [@username, @password].compact.join(':') if @username || @password
  end

end
cast_to_integer_or_nil(object) click to toggle source
# File lib/rails_db_config_resolver/database_config.rb, line 73
def cast_to_integer_or_nil(object)
  return nil unless object.is_a?(Numeric) || /^\d+$/.match(object)
  object.to_i
end