Class: ActiveSupport::EnvConfiguration
- Defined in:
- activesupport/lib/active_support/env_configuration.rb
Overview
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 }
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize ⇒ EnvConfiguration
constructor
A new instance of EnvConfiguration.
-
#inspect ⇒ Object
:nodoc:.
-
#keys ⇒ Object
Returns an array of symbolized keys from the environment variables.
-
#option(*key, default: nil) ⇒ Object
Find an upcased and double-underscored-joined string-version of the
keyin ENV. -
#reload ⇒ Object
Reload the cached ENV values in case any of them changed or new ones were added during runtime.
-
#require(*key) ⇒ Object
Find an upcased and double-underscored-joined string-version of the
keyin ENV.
Constructor Details
#initialize ⇒ EnvConfiguration
Returns a new instance of EnvConfiguration.
20 21 22 |
# File 'activesupport/lib/active_support/env_configuration.rb', line 20 def initialize reload end |
Instance Method Details
#inspect ⇒ Object
:nodoc:
73 74 75 |
# File 'activesupport/lib/active_support/env_configuration.rb', line 73 def inspect # :nodoc: "#<#{self.class.name}:#{'%#016x' % (object_id << 1)} keys=#{keys.inspect}>" end |
#keys ⇒ Object
Returns an array of symbolized keys from the environment variables.
Examples:
ENV["DB_HOST"] = "localhost"
ENV["DATABASE_PORT"] = "5432"
env_config = ActiveSupport::EnvConfiguration.new
env_config.keys
# => [:db_host, :database_port, ...]
69 70 71 |
# File 'activesupport/lib/active_support/env_configuration.rb', line 69 def keys @envs.keys.map { |k| k.downcase.to_sym } end |
#option(*key, default: nil) ⇒ Object
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 }
45 46 47 48 49 50 51 |
# File 'activesupport/lib/active_support/env_configuration.rb', line 45 def option(*key, default: nil) if default.respond_to?(:call) @envs.fetch(envify(*key)) { default.call } else @envs.fetch envify(*key), default end end |
#reload ⇒ Object
Reload the cached ENV values in case any of them changed or new ones were added during runtime.
54 55 56 |
# File 'activesupport/lib/active_support/env_configuration.rb', line 54 def reload @envs = ENV.to_h end |
#require(*key) ⇒ Object
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")
31 32 33 |
# File 'activesupport/lib/active_support/env_configuration.rb', line 31 def require(*key) @envs.fetch envify(*key) end |