Module: GemHadar::Utils

Included in:
GemHadar
Defined in:
lib/gem_hadar/utils.rb

Overview

A module that provides utility methods for handling XDG Base Directory specification compliance.

This module offers functionality to determine the appropriate configuration directory based on the XDG Base Directory specification, construct paths for configuration files, and retrieve configuration data from those files. It helps ensure consistent and standardized handling of user configuration files across different operating systems.

Examples:

Determining the XDG configuration directory

config_dir = GemHadar::Utils.xdg_config_home

Retrieving configuration file contents

config_content = GemHadar::Utils.xdg_config('myapp', 'default_value')

Instance Method Summary collapse

Instance Method Details

#xdg_config_filename(name) ⇒ String

The xdg_config_filename method constructs the full path to a configuration file based on the XDG Base Directory specification.

It first checks if the XDG_CONFIG_HOME environment variable is set and not empty. If it is set, the method joins this directory with the provided filename to form the complete path. If XDG_CONFIG_HOME is not set, it defaults to using the HOME environment variable to construct the path within the standard .config directory.

Parameters:

  • name (String)

    the name of the configuration file

Returns:

  • (String)

    the full path to the configuration file



44
45
46
# File 'lib/gem_hadar/utils.rb', line 44

def xdg_config_filename(name)
  xdg_config_home + name
end

#xdg_config_homePathname

The xdg_config_home method determines the path to the XDG configuration directory.

It first checks if the XDG_CONFIG_HOME environment variable is set and not empty. If it is set, the method returns the value as a Pathname object. If XDG_CONFIG_HOME is not set, it defaults to using the HOME environment variable to construct the path within the standard .config directory.

Returns:

  • (Pathname)

    the Pathname object representing the XDG configuration directory



27
28
29
30
# File 'lib/gem_hadar/utils.rb', line 27

def xdg_config_home
  ENV['XDG_CONFIG_HOME'].full? { Pathname.new(_1) } ||
    Pathname.new(ENV.fetch('HOME')) + '.config'
end