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
-
#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.
Constructor Details
.new(*configurations) ⇒ CombinedConfiguration
# File 'activesupport/lib/active_support/combined_configuration.rb', line 12
def initialize(*configurations) @configurations = configurations end
Instance Method Details
#option(*key, default: nil)
Find singular or nested keys across all backends. If no backend holds the key, nil is returned. If a default value is defined, it (or its callable value) will be returned on a missing key.
Examples:
option(:db_host) # => ENV["DB_HOST"] || Rails.app.credentials.option(:db_host)
option(:database, :host) # => ENV["DATABASE__HOST"] || Rails.app.credentials.option(:database, :host)
option(:database, :host, default: "missing") # => ENV["DATABASE__HOST"] || Rails.app.credentials.option(:database, :host) || "missing"
option(:database, :host, default: -> { "missing" }) # => ENV["DATABASE__HOST"] || Rails.app.credentials.option(:database, :host) || "missing"
# File 'activesupport/lib/active_support/combined_configuration.rb', line 40
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 50
def reload @configurations.each { |config| config.try(:reload) } end
#require(*key)
Find singular or nested keys across all backends. If no backend holds the key, it raises KeyError.
Examples of Rails-configured access:
require(:db_host) # => ENV["DB_HOST"] || Rails.app.credentials.require(:db_host)
require(:database, :host) # => ENV["DATABASE__HOST"] || Rails.app.credentials.require(:database, :host)
# File 'activesupport/lib/active_support/combined_configuration.rb', line 22
def require(*key) @configurations.each do |config| value = config.option(*key) return value unless value.nil? end raise KeyError, "Missing key: #{key.inspect}" end