Provide a better interface for accessing configuration options stored in ENV. Keys are accepted as symbols and turned into upcased strings. Nesting is provided by double underscores.
This interface mirrors what is used for ActiveSupport::EncryptedConfiguration and thus allows both to serve as interchangeable backends for Rails.app.credentials.
Examples:
require(:db_host) # => ENV.fetch("DB_HOST") require(:database, :host) # => ENV.fetch("DATABASE__HOST") option(:database, :host) # => ENV["DATABASE__HOST"] option(:debug, default: "true") # => ENV.fetch("DB_HOST") { "true" } option(:database, :host, default: -> { "missing" }) # => ENV.fetch("DATABASE__HOST") { default.call }
Class Public methods
new() Link
Instance Public methods
option(*key, default: nil) Link
Find an upcased and double-underscored-joined string-version of the key in ENV. Returns nil if the key isn’t found or the value of default when passed. If default is a callable, it’s called first.
Examples:
option(:db_host) # => ENV["DB_HOST"] option(:database, :host) # => ENV["DATABASE__HOST"] option(:database, :host, default: "missing") # => ENV.fetch("DATABASE__HOST", "missing") option(:database, :host, default: -> { "missing" }) # => ENV.fetch("DATABASE__HOST") { default.call }
reload() Link
Reload the cached ENV values in case any of them changed or new ones were added during runtime.
require(*key) Link
Find an upcased and double-underscored-joined string-version of the key in ENV. Raises KeyError if not found.
Examples:
require(:db_host) # => ENV.fetch("DB_HOST") require(:database, :host) # => ENV.fetch("DATABASE__HOST")