123456789_123456789_123456789_123456789_123456789_

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

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

[ GitHub ]

  
# File 'activesupport/lib/active_support/combined_configuration.rb', line 12

def initialize(*configurations)
  @configurations = configurations
end

Instance Method Details

#inspect

This method is for internal use only.
[ GitHub ]

  
# 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

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]
[ GitHub ]

  
# 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)
[ GitHub ]

  
# 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.

[ GitHub ]

  
# 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)

Raises:

  • (KeyError)
[ GitHub ]

  
# 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