Module: ConfigSL::FromEnvironment
- Included in:
- Config
- Defined in:
- lib/configsl/from_environment.rb
Overview
Load configuration from the environment.
This module provides a way to load configuration values from environment
variables. It checks for a variables using the name of the option, in upper
snake case (e.g. MY_OPTION). You can add a prefix to all variable names
using from_environment_prefix.
from_environment_prefix 'DATABASE_'
option :host, default: 'localhost'
You can override the variable name for individual options by setting
env_variable.
option :host, default: 'localhost', env_variable: 'DB_HOST'
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.configsl_load_params(klass, opts = {}) ⇒ Hash
Loads the configuration parameters from environment variables.
- .configsl_source_name ⇒ Object
- .included(base) ⇒ Object
Class Method Details
.configsl_load_params(klass, opts = {}) ⇒ Hash
Loads the configuration parameters from environment variables.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/configsl/from_environment.rb', line 35 def self.configsl_load_params(klass, opts = {}) klass..each_with_object({}) do |(name, option_opts), hash| env_var = option_opts[:env_variable] || name.to_s.upcase if ENV.key?(env_var) hash[name] = ENV[env_var] elsif opts[:defaults] hash[name] = option_opts[:default] end end end |
.configsl_source_name ⇒ Object
24 25 26 |
# File 'lib/configsl/from_environment.rb', line 24 def self.configsl_source_name :environment end |
.included(base) ⇒ Object
19 20 21 22 |
# File 'lib/configsl/from_environment.rb', line 19 def self.included(base) base.extend ClassMethods base.from_environment_prefix '' end |