Module: ConstConf::DirPlugin

Defined in:
lib/const_conf/dir_plugin.rb

Overview

A module that provides functionality for reading file contents from configuration directories, supporting XDG Base Directory Specification compliance.

The DirPlugin extends the ConstConf::Setting class to enable configuration settings that are sourced from files within named directories. This allows for more complex and secure configuration management, particularly useful for storing sensitive data or structured configs in files rather than environment variables.

Examples:

Basic usage with file reading

module APP
  include ConstConf
  plugin ConstConf::DirPlugin

  CONFIG_FILE = set do
    description 'Configuration from file'
    default dir('myapp', 'config.yaml')
    decoding { |s| YAML.load(s) }
  end
end

XDG-compliant directory structure

module APP
  include ConstConf
  plugin ConstConf::DirPlugin

  XDG_CONFIG_HOME = set do
    description 'XDG Config HOME'
    prefix ''
  end

  API_KEY = set do
    description 'API Key from XDG config'
    default dir('myapp', 'api_key.txt', env_var: APP::XDG_CONFIG_HOME?)
  end
end

See Also:

Defined Under Namespace

Classes: ConfigDir

Instance Method Summary collapse

Instance Method Details

#dir(name, path, env_var: nil, env_var_name: nil, default: nil, required: false) ⇒ Object

The dir method creates and reads a configuration directory setting.

This method initializes a ConfigDir instance with the provided name and environment variable configuration, then reads the directory path with optional default and required validation settings.

configuration provided

Parameters:

  • name (String)

    the name of the configuration directory

  • path (String)

    the filesystem path to the directory

  • env_var (String, nil) (defaults to: nil)

    the environment variable name to use for

  • env_var_name (String, nil) (defaults to: nil)

    the full environment variable name to use

  • default (Object) (defaults to: nil)

    the default value to use when no configuration is

  • required (Boolean) (defaults to: false)

    whether the directory path is required to exist

Returns:

  • (Object)

    the result of reading path from the directory name



178
179
180
# File 'lib/const_conf/dir_plugin.rb', line 178

def dir(name, path, env_var: nil, env_var_name: nil, default: nil, required: false)
  ConfigDir.new(name, env_var:, env_var_name:).read(path, default:, required:)
end