Class: ActiveSupport::CombinedConfiguration
- 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.
Instance Method Summary collapse
-
#initialize(*configurations) ⇒ CombinedConfiguration
constructor
A new instance of CombinedConfiguration.
-
#inspect ⇒ Object
:nodoc:.
-
#keys ⇒ Object
Returns a unique array of all symbolized keys available across all backend configurations.
-
#option(*key, default: nil) ⇒ Object
Find singular or nested keys across all backends.
-
#reload ⇒ Object
Reload the cached values for all of the backend configurations.
-
#require(*key) ⇒ Object
Find singular or nested keys across all backends.
Constructor Details
#initialize(*configurations) ⇒ CombinedConfiguration
Returns a new instance of CombinedConfiguration.
12 13 14 |
# File 'activesupport/lib/active_support/combined_configuration.rb', line 12 def initialize(*configurations) @configurations = configurations end |
Instance Method Details
#inspect ⇒ Object
:nodoc:
68 69 70 |
# File 'activesupport/lib/active_support/combined_configuration.rb', line 68 def inspect # :nodoc: "#<#{self.class.name}:#{'%#016x' % (object_id << 1)} keys=#{keys.inspect}>" end |
#keys ⇒ Object
64 65 66 |
# File 'activesupport/lib/active_support/combined_configuration.rb', line 64 def keys @configurations.flat_map(&:keys).uniq end |
#option(*key, default: nil) ⇒ Object
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"
40 41 42 43 44 45 46 47 |
# 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 ⇒ Object
Reload the cached values for all of the backend configurations.
50 51 52 |
# File 'activesupport/lib/active_support/combined_configuration.rb', line 50 def reload @configurations.each { |config| config.try(:reload) } end |
#require(*key) ⇒ Object
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)
22 23 24 25 26 27 28 29 |
# 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 |