123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::EnvConfiguration

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
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 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("DEBUG", "true")
option(:database, :host, default: -> { "missing" }) # => ENV.fetch("DATABASE__HOST") { "missing" }

Class Method Summary

Instance Method Summary

Constructor Details

.newEnvConfiguration

[ GitHub ]

  
# File 'activesupport/lib/active_support/env_configuration.rb', line 18

def initialize
  reload
end

Instance Method Details

#envify(*key) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/env_configuration.rb', line 87

def envify(*key)
  key.collect { |part| part.to_s.upcase }.join("__")
end

#inspect

This method is for internal use only.
[ GitHub ]

  
# File 'activesupport/lib/active_support/env_configuration.rb', line 78

def inspect # :nodoc:
  "#<#{self.class.name}:#{'%#016x' % (object_id << 1)} keys=#{keys.inspect}>"
end

#keys

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

  
# File 'activesupport/lib/active_support/env_configuration.rb', line 74

def keys
  @envs.keys.map { |k| k.downcase.to_sym }
end

#lookup(env_key) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/env_configuration.rb', line 83

def lookup(env_key)
  @envs[env_key]
end

#option(*key, default: nil)

Find an upcased and double-underscored-joined string-version of the key in ENV. Returns nil if the key isn't found. If a default value is defined, it (or its callable value) will be returned on a missing key.

Given ENV:

DB_HOST: "env.example.com"
DATABASE__HOST: "env.example.com"

Examples:

option(:db_host)                              # => "env.example.com"
option(:missing)                              # => nil
option(:missing, default: "localhost")        # => "localhost"
option(:missing, default: -> { "localhost" }) # => "localhost"
[ GitHub ]

  
# File 'activesupport/lib/active_support/env_configuration.rb', line 50

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

Reload the cached ENV values in case any of them changed or new ones were added during runtime.

[ GitHub ]

  
# File 'activesupport/lib/active_support/env_configuration.rb', line 59

def reload
  @envs = ENV.to_h
end

#require(*key)

Find an upcased and double-underscored-joined string-version of the key in ENV. Raises KeyError if not found.

Given ENV:

DB_HOST: "env.example.com"
DATABASE__HOST: "env.example.com"

Examples:

require(:db_host)         # => "env.example.com"
require(:database, :host) # => "env.example.com"
require(:missing)         # => KeyError
[ GitHub ]

  
# File 'activesupport/lib/active_support/env_configuration.rb', line 33

def require(*key)
  @envs.fetch envify(*key)
end