Class: ConstConf::DirPlugin::ConfigDir

Inherits:
Object
  • Object
show all
Defined in:
lib/const_conf/dir_plugin.rb

Overview

A configuration directory handler that provides methods for deriving directory paths, joining paths, and reading file contents from a specified directory structure.

The ConfigDir class is designed to work with the XDG Base Directory Specification and supports reading files from directories with optional environment variable configuration for the root path. It provides a clean interface for accessing configuration files in a structured way.

Instance Method Summary collapse

Constructor Details

#initialize(name, root_path: nil, env_var:, env_var_name: nil) ⇒ ConfigDir

Initializes a new instance with a name and environment variable configuration.

directory path look up

Parameters:

  • name (String)

    the name used to derive the directory path

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

    the root path to use for deriving the

  • env_var (String, nil)

    the environment variable value to use

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

    the name of the environment variable to

Raises:

  • (ArgumentError)

    if env_var and env_var_name were given.



61
62
63
64
65
66
67
68
69
70
# File 'lib/const_conf/dir_plugin.rb', line 61

def initialize(name, root_path: nil, env_var:, env_var_name: nil)
  !env_var.nil? && !env_var_name.nil? and
    raise ArgumentError,
    "need either the value of an env_var or the name env_var_name of an env_var"
  if env_var.nil? && !env_var_name.nil?
    env_var = ENV[env_var_name]
  end
  root_path ||= env_var
  @directory_path = derive_directory_path(name, root_path)
end

Instance Method Details

#join(path) ⇒ Pathname Also known as: +

Joins the directory path with the given path and returns the combined result.

Parameters:

  • path (String)

    the path to be joined with the directory path

Returns:

  • (Pathname)

    the combined path as a Pathname object



85
86
87
# File 'lib/const_conf/dir_plugin.rb', line 85

def join(path)
  @directory_path + path
end

#read(path, default: nil, required: false) {|io| ... } ⇒ String?

Reads the content of a file at the given path within the configuration directory.

If the file exists, it returns the file’s content as a string encoded in UTF-8. If a block is given and the file exists, it opens the file and yields to the block. If the file does not exist and a default value is provided, it returns the default. If a block is given and the file does not exist, it yields a StringIO object containing the default value to the block.

directory does not exist

the file does not exist

Parameters:

  • path (String)

    the path to the file relative to the configuration

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

    the default value to return if the file

Yields:

  • (io)

Returns:

  • (String, nil)

    the content of the file or the default value if



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/const_conf/dir_plugin.rb', line 110

def read(path, default: nil, required: false, &block)
  full_path = join(path)
  if File.exist?(full_path)
    if block
      File.open(full_path, &block)
    else
      File.read(full_path, encoding: 'UTF-8')
    end
  else
    required and raise ConstConf::RequiredValueNotConfigured,
      "require file at #{full_path.to_s.inspect}"
    if default && block
      block.(StringIO.new(default))
    else
      default
    end
  end
end

#to_sString

Returns the string representation of the configuration directory path.

Returns:

  • (String)

    the path of the configuration directory as a string



75
76
77
# File 'lib/const_conf/dir_plugin.rb', line 75

def to_s
  @directory_path.to_s
end