Skip to Content Skip to Search

Allows for configuration keys to be pulled from multiple backends. Keys are pulled in first-found order from the configuration backends that the combined configuration has been initialized with.

This is used by Rails to offer a unified API for fetching credentials from both ENV and the encrypted file. You can access this through Rails.app.creds within a Rails application.

Methods
K
N
O
R

Class Public methods

new(*configurations)

# File activesupport/lib/active_support/combined_configuration.rb, line 12
def initialize(*configurations)
  @configurations = configurations
end

Instance Public methods

keys()

Returns a unique array of all symbolized keys available across all backend configurations.

Examples of Rails-configured access:

ENV["DB_HOST"] = "localhost"
Rails.app.credentials # has keys [:secret_key_base, :aws]

Rails.app.creds.keys
# => [:db_host, :secret_key_base, :aws]
# File activesupport/lib/active_support/combined_configuration.rb, line 85
def keys
  @configurations.flat_map(&:keys).uniq
end

option(*key, default: nil)

Find singular or nested keys across all backends. Returns nil if no backend holds the key. If a default value is defined, it (or its callable value) will be returned on a missing key or nil value.

Given ENV:

DATABASE__HOST: "env.example.com"

And credentials:

database:
  host: "creds.example.com"
api_key: "secret"
api_host: null

Examples:

option(:database, :host)                      # => "env.example.com" (ENV overrides credentials)
option(:missing)                              # => nil
option(:missing, default: "localhost")        # => "localhost"
option(:missing, default: -> { "localhost" }) # => "localhost"
option(:api_host, default: "api.example.com") # => "api.example.com" (nil values use default)
# File activesupport/lib/active_support/combined_configuration.rb, line 61
def option(*key, default: nil)
  @configurations.each do |config|
    value = config.option(*key)
    return value unless value.nil?
  end

  default.respond_to?(:call) ? default.call : default
end

reload()

Reload the cached values for all of the backend configurations.

# File activesupport/lib/active_support/combined_configuration.rb, line 71
def reload
  @configurations.each { |config| config.try(:reload) }
end

require(*key)

Find singular or nested keys across all backends. Raises KeyError if no backend holds the key or if the value is nil.

Given ENV:

DATABASE__HOST: "env.example.com"

And credentials:

database:
  host: "creds.example.com"
api_key: "secret"
api_host: null

Examples:

require(:database, :host) # => "env.example.com" (ENV overrides credentials)
require(:api_key)         # => "secret" (from credentials)
require(:missing)         # => KeyError
require(:api_host)        # => KeyError (nil values are treated as missing)
# File activesupport/lib/active_support/combined_configuration.rb, line 33
def require(*key)
  @configurations.each do |config|
    value = config.option(*key)
    return value unless value.nil?
  end

  raise KeyError, "Missing key: #{key.inspect}"
end