Class: ActiveSupport::DotEnvConfiguration
- Inherits:
-
EnvConfiguration
- Object
- EnvConfiguration
- ActiveSupport::DotEnvConfiguration
- Defined in:
- activesupport/lib/active_support/dot_env_configuration.rb
Overview
Provide an interface for accessing configuration options stored in a .env file. Keys are accepted as symbols and turned into upcased strings. Nesting is provided by double underscores.
This interface mirrors what is used for ActiveSupport::EnvConfiguration and ActiveSupport::EncryptedConfiguration and thus allows all three to serve as interchangeable backends for Rails.app.creds.
The .env file format supports:
-
Lines with KEY=value pairs
-
Comments starting with #
-
Empty lines (ignored)
-
Quoted values (single or double quotes)
-
Variable interpolation with $VAR syntax
-
Command execution with $(command) syntax
The command execution allows for easy integration with third-party credential providers, like 1password:
DB_HOST=$(op read op://Vault/item/value --account=MyAccount)
When used inside Rails, the default path for the .env file will be ‘Rails.root.join(“.env”)`. Otherwise it must be passed in as path.
Examples:
require(:db_host) # => value of DB_HOST from .env
require(:database, :host) # => value of DATABASE__HOST from .env
option(:debug) # => value of DEBUG from .env or nil if missing
option(:debug, default: "true") # => value of DEBUG from .env or "true" if not found
option(:database, :host, default: -> { "missing" }) # => value of DATABASE__HOST from .env or "missing" if not found
Instance Method Summary collapse
-
#initialize(path) ⇒ DotEnvConfiguration
constructor
A new instance of DotEnvConfiguration.
-
#reload ⇒ Object
Reload the cached .env values in case the file changed during runtime.
Methods inherited from EnvConfiguration
#inspect, #keys, #option, #require
Constructor Details
#initialize(path) ⇒ DotEnvConfiguration
Returns a new instance of DotEnvConfiguration.
35 36 37 38 |
# File 'activesupport/lib/active_support/dot_env_configuration.rb', line 35 def initialize(path) @path = path reload end |
Instance Method Details
#reload ⇒ Object
Reload the cached .env values in case the file changed during runtime.
41 42 43 |
# File 'activesupport/lib/active_support/dot_env_configuration.rb', line 41 def reload @envs = parse_env_file end |