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

Class Method Details

.configsl_load_params(klass, opts = {}) ⇒ Hash

Loads the configuration parameters from environment variables.

Parameters:

  • klass (Class)

    The class loading the configuration.

  • opts (Hash) (defaults to: {})

    Options for loading the configuration.

Options Hash (opts):

  • :defaults (Boolean)

    Whether to populate default values, defaults to true

Returns:

  • (Hash)

    Loaded configuration parameters.



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.options.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_nameObject



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