Class: ConfigX::ConfigFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/config_x/config_factory.rb

Overview

This class is responsible for loading configuration settings for an application. It follows a specific order in loading these settings:

1. Reads default config
2. Reads all the config files provided in the order
3. Reads environment variables

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = self.class.default_env, env_prefix: self.class.default_env_prefix, env_separator: self.class.default_env_separator, dir_name: self.class.default_dir_name, file_name: self.class.default_file_name, config_root: self.class.default_config_root, config_class: self.class.default_config_class) ⇒ ConfigFactory

Initializes a new instance of the ConfigFactory class.

Parameters:

  • env (String) (defaults to: self.class.default_env)

    the environment name.

  • env_prefix (String) (defaults to: self.class.default_env_prefix)

    the prefix for environment variables.

  • env_separator (String) (defaults to: self.class.default_env_separator)

    the separator for environment variables.

  • dir_name (String) (defaults to: self.class.default_dir_name)

    the directory name for settings.

  • file_name (String) (defaults to: self.class.default_file_name)

    the file name for settings.

  • config_root (String) (defaults to: self.class.default_config_root)

    the root directory for configuration.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/config_x/config_factory.rb', line 44

def initialize(
  env = self.class.default_env,
  env_prefix: self.class.default_env_prefix,
  env_separator: self.class.default_env_separator,
  dir_name: self.class.default_dir_name,
  file_name: self.class.default_file_name,
  config_root: self.class.default_config_root,
  config_class: self.class.default_config_class
)
  @env = env
  @env_prefix = env_prefix
  @env_separator = env_separator
  @dir_name = dir_name
  @file_name = file_name
  @config_root = config_root
  @config_class = config_class
end

Class Method Details

.default_config_classClass<ConfigX::Configurable>

Default configuration class

Returns:



31
# File 'lib/config_x/config_factory.rb', line 31

def default_config_class = UntypedConfig

.default_config_rootObject

Default root directory for configuration



27
# File 'lib/config_x/config_factory.rb', line 27

def default_config_root = "config"

.default_dir_nameObject

Default directory name for environment-specific settings



18
# File 'lib/config_x/config_factory.rb', line 18

def default_dir_name = "settings"

.default_envObject

Default environment name



21
# File 'lib/config_x/config_factory.rb', line 21

def default_env = "production"

.default_env_prefixObject

Default environment variable prefix



12
# File 'lib/config_x/config_factory.rb', line 12

def default_env_prefix = "SETTINGS"

.default_env_separatorObject

Default environment variable separator



15
# File 'lib/config_x/config_factory.rb', line 15

def default_env_separator = "__"

.default_file_nameObject

Default config file name



24
# File 'lib/config_x/config_factory.rb', line 24

def default_file_name = "settings"

.loadObject

Load method to initialize and load the configuration



34
# File 'lib/config_x/config_factory.rb', line 34

def load(...) = new(...).load

Instance Method Details

#load(*additional_sources) ⇒ UntypedConfig

Loads the configuration from the sources and additional sources.

Parameters:

  • additional_sources (Array)

    additional sources to load configuration from.

Returns:



65
66
67
68
69
# File 'lib/config_x/config_factory.rb', line 65

def load(*additional_sources)
  (sources + additional_sources)
    .reduce(Builder.new) { |builder, source| builder.add_source(source) }
    .load(config_class:)
end