class Utils::ConfigDir

config_dir.read(‘missing.txt’, default: ‘default content’) # => returns ‘default content’ if file is missing
config_dir.read(‘settings.rb’) # => reads and returns the content of ‘settings.rb’ or nil if not found
config_dir.join(‘config.txt’) # => returns a Pathname object for the joined path
config_dir.to_s # => returns the string representation of the configuration directory path
config_dir = Utils::ConfigDir.new(‘myapp’)
@example
robust path manipulation.
environment variable-based root path resolution and uses Pathname for
read files with optional default values and block handling. It supports
deriving paths based on a root directory and name, and offering methods to
This class provides functionality for managing configuration directories by
operations within a specified directory structure.
A configuration directory manager that handles path resolution and file

def default_root_path

Returns:
  • (Pathname) - the default configuration directory path
def default_root_path
  Pathname.new(ENV.fetch('HOME') + '.config')
end

def derive_directory_path(name, root_path)

Returns:
  • (Pathname) - the combined directory path as a Pathname object

Parameters:
  • root_path (String, nil) -- the root path to use; if nil, the default root path is used
  • name (String) -- the name of the directory to be appended to the root path
def derive_directory_path(name, root_path)
  root = if path = root_path
           Pathname.new(path)
         else
           Pathname.new(default_root_path)
         end
  root + name
end

def env_var_path(env_var)

Returns:
  • (String, nil) - the value of the environment variable if it

Parameters:
  • env_var (String) -- the name of the environment variable to check
def env_var_path(env_var)
  env_var.full? { ENV[it].full? }
end

def initialize(name, root_path: nil, env_var: nil)

Parameters:
  • env_var (String, nil) -- the name of the environment variable to
  • root_path (String, nil) -- the root path to use; if nil, the
  • name (String) -- the name of the directory to be used
def initialize(name, root_path: nil, env_var: nil)
  root_path ||= env_var_path(env_var)
  @directory_path = derive_directory_path(name, root_path)
end

def initialize(name, root_path: nil, env_var: nil)

Parameters:
  • env_var (String, nil) -- the name of the environment variable to
  • root_path (String, nil) -- the root path to use; if nil, the
  • name (String) -- the name of the directory to be used
def initialize(name, root_path: nil, env_var: nil)
  root_path ||= env_var_path(env_var)
  @directory_path = derive_directory_path(name, root_path)
end

def join(path)

Returns:
  • (Pathname) - the combined path as a Pathname object

Parameters:
  • path (String) -- the path to be joined with the directory path
def join(path)
  @directory_path + path
end

def read(path, default: nil, &block)

Returns:
  • (String, nil) - the content of the file or the default value if

Other tags:
    Yield: -

Parameters:
  • default (String, nil) -- the default value to return if the file
  • path (String) -- the path to the file relative to the configuration
def read(path, default: nil, &block)
  full_path = join(path)
  if File.exist?(full_path)
    if block
      File.new(full_path, &block)
    else
      File.read(full_path, encoding: 'UTF-8')
    end
  else
    if default && block
      block.(StringIO.new(default))
    else
      default
    end
  end
end

def to_s

Returns:
  • (String) - the path of the configuration directory as a string
def to_s
  @directory_path.to_s
end