class Utils::ConfigDir

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