Module: ConstConf::YAMLPlugin

Includes:
ComplexConfig::Provider::Shortcuts
Defined in:
lib/const_conf/yaml_plugin.rb

Overview

A module that provides functionality for reading and parsing YAML file contents as configuration values.

The YAMLPlugin module extends the ConstConf::Setting class to enable configuration settings that are sourced from YAML files. This allows for structured configuration data to be loaded from files and used within the application’s configuration system, supporting both standard YAML parsing and environment-specific configuration loading.

Instance Method Summary collapse

Instance Method Details

#yaml(path, required: false, env: false) ⇒ Object?

Reads and parses a YAML file, optionally with environment-specific loading

This method attempts to read and parse a YAML file at the specified path. It supports environment-specific configuration loading when the env parameter is provided. The method uses thread synchronization to ensure safe access to the ComplexConfig provider.

the file doesn’t exist and required is false

and required is true is not set and no explicit environment is provided

Parameters:

  • path (String)

    the filesystem path to the YAML file to be read

  • required (Boolean) (defaults to: false)

    whether the file is required to exist, defaults to false

  • env (Boolean, String) (defaults to: false)

    whether to load environment-specific configuration, can be true to use RAILS_ENV or a specific environment string

Returns:

  • (Object, nil)

    the parsed YAML content as a Ruby object, or nil if

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/const_conf/yaml_plugin.rb', line 33

def yaml(path, required: false, env: false)
  if File.exist?(path)
    ConstConf.monitor.synchronize do
      config_dir = File.dirname(path)
      ComplexConfig::Provider.config_dir = config_dir
      ext  = File.extname(path)
      name = File.basename(path, ext)
      if env
        env == true and env = ENV['RAILS_ENV']
        if env
          complex_config_with_env(name, env)
        else
          raise ConstConf::RequiredValueNotConfigured,
            "need an environment string specified, via env var RAILS_ENV or manually"
        end
      else
        complex_config(name)
      end
    end
  elsif required
    raise ConstConf::RequiredValueNotConfigured,
      "YAML file required at path #{path.to_s.inspect}"
  end
end