Module: DbCharmerEnvUrls

Defined in:
lib/db_charmer_env_urls.rb,
lib/db_charmer_env_urls/engine.rb,
lib/db_charmer_env_urls/version.rb

Defined Under Namespace

Classes: Engine

Constant Summary collapse

VERSION =
"1.1.2"

Class Method Summary collapse

Class Method Details

.database_configurationsHash

Extracts database configs from the ENV where ENV key ends it _DATABASE_URL

Examples:

Matching ENV variable names

SLAVE_DATABASE_URL, CRIMSON_DATABASE_URL

Usage

ENV["SLAVE_DATABASE_URL"] = "postgres://localhost:5432/name"
DbCharmerEnvUrls.database_configurations
{
  "slave" => {
    "adapter" => "postgres",
    "host" => "localhost",
    "port" => 5432,
    "database" => "name"
  }
}

Returns:

  • (Hash)

    Hash of database configurations whose keys are extracted from the ENV and whose values are hashes of database configs



23
24
25
26
27
28
29
30
31
32
# File 'lib/db_charmer_env_urls.rb', line 23

def self.database_configurations
  base = ActiveRecord::Base
  ENV.inject({}) do |acc, (key, url)|
    if matches = key.match(/\A(\w+)_DATABASE_URL\z/)
      config = base::ConnectionSpecification::Resolver.new(url, base.configurations).spec.config
      acc[matches[1].downcase] = config.stringify_keys
    end
    acc
  end
end