Class: ActiveSupport::CombinedConfiguration
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | activesupport/lib/active_support/combined_configuration.rb |
Overview
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.
Class Method Summary
- .new(*configurations) ⇒ CombinedConfiguration constructor
Instance Method Summary
-
#keys
Returns a unique array of all symbolized keys available across all backend configurations.
-
#option(*key, default: nil)
Find singular or nested keys across all backends.
-
#reload
Reload the cached values for all of the backend configurations.
-
#require(*key)
Find singular or nested keys across all backends.
- #inspect Internal use only
Constructor Details
.new(*configurations) ⇒ CombinedConfiguration
# File 'activesupport/lib/active_support/combined_configuration.rb', line 12
def initialize(*configurations) @configurations = configurations end
Instance Method Details
#inspect
# File 'activesupport/lib/active_support/combined_configuration.rb', line 89
def inspect # :nodoc: "#<#{self.class.name}:#{'%#016x' % (object_id << 1)} keys=#{keys.inspect}>" end
#keys
# 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)