Class: Toys::Utils::XDG
- Inherits:
-
Object
- Object
- Toys::Utils::XDG
- Defined in:
- core-docs/toys/utils/xdg.rb,
core-docs/toys/utils/xdg.rb
Overview
Defined in the toys-core gem
A class that provides tools for working with the XDG Base Directory Specification.
This class provides utility methods that locate base directories and search paths for application state, configuration, caches, and other data, according to the XDG Base Directory Spec version 0.8.
Example
require "toys/utils/xdg"
xdg = Toys::Utils::XDG.new
# Get config file paths, in order from most to least important
config_files = xdg.lookup_config("my-config.toml")
config_files.each { |path| read_my_config(path) }
Windows operation
The Spec assumes a unix-like environment, and cannot be applied directly to Windows without modification. In general, this class will function on Windows, but with the following caveats:
- All file paths must use Windows-style absolute paths, beginning with the drive letter.
- Environment variables that can contain multiple paths (
XDG_*_DIRS) use the Windows path delimiter (;) rather than the unix path delimiter (:). - Defaults for home directories (
XDG_*_HOME) will follow unix conventions, using subdirectories under the user's profile directory rather than the Windows known folder paths. - Defaults for search paths (
XDG_*_DIRS) will be empty and will not use the Windows known folder paths.
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
Version of the simple_xdg gem
"0.1.1"
Instance Method Summary collapse
-
#cache_home ⇒ String
Returns the absolute path to the single base directory relative to which user-specific non-essential (cached) data should be written.
-
#config_dirs ⇒ Array<String>
Returns the set of preference ordered base directories relative to which configuration files should be searched, as an array of absolute paths.
-
#config_home ⇒ String
Returns the absolute path to the single base directory relative to which user-specific configuration files should be written.
-
#data_dirs ⇒ Array<String>
Returns the set of preference ordered base directories relative to which data files should be searched, as an array of absolute paths.
-
#data_home ⇒ String
Returns the absolute path to the single base directory relative to which user-specific data files should be written.
-
#ensure_cache_subdir(path) ⇒ String
Returns the absolute path to a directory under #cache_home, creating it if it doesn't already exist.
-
#ensure_config_subdir(path) ⇒ String
Returns the absolute path to a directory under #config_home, creating it if it doesn't already exist.
-
#ensure_data_subdir(path) ⇒ String
Returns the absolute path to a directory under #data_home, creating it if it doesn't already exist.
-
#ensure_state_subdir(path) ⇒ String
Returns the absolute path to a directory under #state_home, creating it if it doesn't already exist.
-
#executable_home ⇒ String
Returns the absolute path to the single base directory relative to which user-specific executable files may be written.
-
#home_dir ⇒ String
Returns the absolute path to the current user's home directory.
-
#initialize(env: ::ENV) ⇒ XDG
constructor
Create an instance of XDG.
-
#lookup_cache(path, type: :file) ⇒ Array<String>
Searches the cache directory (#cache_home) for an object with the given relative path, and returns an array of zero or one absolute paths to any found object.
-
#lookup_config(path, type: :file) ⇒ Array<String>
Searches the config directories for an object with the given relative path, and returns an array of absolute paths to all objects found in all config directories (i.e. #config_home and #config_dirs), in order from most to least important.
-
#lookup_data(path, type: :file) ⇒ Array<String>
Searches the data directories for an object with the given relative path, and returns an array of absolute paths to all objects found in all data directories (i.e. #data_home and #data_dirs), in order from most to least important.
-
#lookup_state(path, type: :file) ⇒ Array<String>
Searches the state directory (#state_home) for an object with the given relative path, and returns an array of zero or one absolute paths to any found object.
-
#runtime_dir ⇒ String?
Returns the absolute path to the single base directory relative to which user-specific runtime files and other file objects should be placed.
-
#runtime_dir! ⇒ String
Returns the absolute path to the single base directory relative to which user-specific runtime files and other file objects should be placed.
-
#state_home ⇒ String
Returns the absolute path to the single base directory relative to which user-specific state files should be written.
Constructor Details
#initialize(env: ::ENV) ⇒ XDG
Create an instance of XDG.
59 60 61 |
# File 'core-docs/toys/utils/xdg.rb', line 59 def initialize(env: ::ENV) # Source available in the toys-core gem end |
Instance Method Details
#cache_home ⇒ String
Returns the absolute path to the single base directory relative to which user-specific non-essential (cached) data should be written.
Corresponds to the value of the $XDG_CACHE_HOME environment variable
and its defaults according to the XDG Base Directory Spec.
120 121 122 |
# File 'core-docs/toys/utils/xdg.rb', line 120 def cache_home # Source available in the toys-core gem end |
#config_dirs ⇒ Array<String>
Returns the set of preference ordered base directories relative to which configuration files should be searched, as an array of absolute paths. The array is ordered from most to least important, and does not include the config home directory.
Corresponds to the value of the $XDG_CONFIG_DIRS environment variable
and its defaults according to the XDG Base Directory Spec.
163 164 165 |
# File 'core-docs/toys/utils/xdg.rb', line 163 def config_dirs # Source available in the toys-core gem end |
#config_home ⇒ String
Returns the absolute path to the single base directory relative to which user-specific configuration files should be written.
Corresponds to the value of the $XDG_CONFIG_HOME environment variable
and its defaults according to the XDG Base Directory Spec.
94 95 96 |
# File 'core-docs/toys/utils/xdg.rb', line 94 def config_home # Source available in the toys-core gem end |
#data_dirs ⇒ Array<String>
Returns the set of preference ordered base directories relative to which data files should be searched, as an array of absolute paths. The array is ordered from most to least important, and does not include the data home directory.
Corresponds to the value of the $XDG_DATA_DIRS environment variable
and its defaults according to the XDG Base Directory Spec.
148 149 150 |
# File 'core-docs/toys/utils/xdg.rb', line 148 def data_dirs # Source available in the toys-core gem end |
#data_home ⇒ String
Returns the absolute path to the single base directory relative to which user-specific data files should be written.
Corresponds to the value of the $XDG_DATA_HOME environment variable
and its defaults according to the XDG Base Directory Spec.
81 82 83 |
# File 'core-docs/toys/utils/xdg.rb', line 81 def data_home # Source available in the toys-core gem end |
#ensure_cache_subdir(path) ⇒ String
Returns the absolute path to a directory under #cache_home, creating it if it doesn't already exist.
352 353 354 |
# File 'core-docs/toys/utils/xdg.rb', line 352 def ensure_cache_subdir(path) # Source available in the toys-core gem end |
#ensure_config_subdir(path) ⇒ String
Returns the absolute path to a directory under #config_home, creating it if it doesn't already exist.
322 323 324 |
# File 'core-docs/toys/utils/xdg.rb', line 322 def ensure_config_subdir(path) # Source available in the toys-core gem end |
#ensure_data_subdir(path) ⇒ String
Returns the absolute path to a directory under #data_home, creating it if it doesn't already exist.
307 308 309 |
# File 'core-docs/toys/utils/xdg.rb', line 307 def ensure_data_subdir(path) # Source available in the toys-core gem end |
#ensure_state_subdir(path) ⇒ String
Returns the absolute path to a directory under #state_home, creating it if it doesn't already exist.
337 338 339 |
# File 'core-docs/toys/utils/xdg.rb', line 337 def ensure_state_subdir(path) # Source available in the toys-core gem end |
#executable_home ⇒ String
Returns the absolute path to the single base directory relative to which user-specific executable files may be written.
Returns the value of $HOME/.local/bin as specified by the XDG Base
Directory Spec.
133 134 135 |
# File 'core-docs/toys/utils/xdg.rb', line 133 def executable_home # Source available in the toys-core gem end |
#home_dir ⇒ String
Returns the absolute path to the current user's home directory.
68 69 70 |
# File 'core-docs/toys/utils/xdg.rb', line 68 def home_dir # Source available in the toys-core gem end |
#lookup_cache(path, type: :file) ⇒ Array<String>
Searches the cache directory (#cache_home) for an object with the
given relative path, and returns an array of zero or one absolute paths
to any found object. Because the XDG basedir spec does not provide for
a list of fallback directories for cache files (i.e. there is no
XDG_CACHE_DIRS variable or list of default paths), this will return a
maximum of one result. However, it returns an array for consistency
with the #lookup_data and #lookup_config methods.
292 293 294 |
# File 'core-docs/toys/utils/xdg.rb', line 292 def lookup_cache(path, type: :file) # Source available in the toys-core gem end |
#lookup_config(path, type: :file) ⇒ Array<String>
Searches the config directories for an object with the given relative path, and returns an array of absolute paths to all objects found in all config directories (i.e. #config_home and #config_dirs), in order from most to least important. Returns the empty array if no suitable objects are found.
If multiple objects are found, the caller should implement its own logic to resolve them. For example, it can select the first (most important) object, or implement logic to combine the contents.
248 249 250 |
# File 'core-docs/toys/utils/xdg.rb', line 248 def lookup_config(path, type: :file) # Source available in the toys-core gem end |
#lookup_data(path, type: :file) ⇒ Array<String>
Searches the data directories for an object with the given relative path, and returns an array of absolute paths to all objects found in all data directories (i.e. #data_home and #data_dirs), in order from most to least important. Returns the empty array if no suitable objects are found.
If multiple objects are found, the caller should implement its own logic to resolve them. For example, it can select the first (most important) object, or implement logic to combine the contents.
224 225 226 |
# File 'core-docs/toys/utils/xdg.rb', line 224 def lookup_data(path, type: :file) # Source available in the toys-core gem end |
#lookup_state(path, type: :file) ⇒ Array<String>
Searches the state directory (#state_home) for an object with the
given relative path, and returns an array of zero or one absolute paths
to any found object. Because the XDG basedir spec does not provide for
a list of fallback directories for state files (i.e. there is no
XDG_STATE_DIRS variable or list of default paths), this will return a
maximum of one result. However, it returns an array for consistency
with the #lookup_data and #lookup_config methods.
270 271 272 |
# File 'core-docs/toys/utils/xdg.rb', line 270 def lookup_state(path, type: :file) # Source available in the toys-core gem end |
#runtime_dir ⇒ String?
Returns the absolute path to the single base directory relative to which user-specific runtime files and other file objects should be placed.
Corresponds to the value of the $XDG_RUNTIME_DIR environment variable
according to the XDG Base Directory Spec.
Important: Returns nil if the $XDG_RUNTIME_DIR environment
variable is unset or invalid. In such a case, it is the caller's
responsibility to determine a fallback strategy, as this library cannot
by itself implement a compliant fallback without OS help.
182 183 184 |
# File 'core-docs/toys/utils/xdg.rb', line 182 def runtime_dir # Source available in the toys-core gem end |
#runtime_dir! ⇒ String
Returns the absolute path to the single base directory relative to which user-specific runtime files and other file objects should be placed.
Corresponds to the value of the $XDG_RUNTIME_DIR environment variable
according to the XDG Base Directory Spec.
Raises Error if the $XDG_RUNTIME_DIR environment
variable is unset or invalid. Unlike #runtime_dir, does not return
nil.
200 201 202 |
# File 'core-docs/toys/utils/xdg.rb', line 200 def runtime_dir! # Source available in the toys-core gem end |
#state_home ⇒ String
Returns the absolute path to the single base directory relative to which user-specific state files should be written.
Corresponds to the value of the $XDG_STATE_HOME environment variable
and its defaults according to the XDG Base Directory Spec.
107 108 109 |
# File 'core-docs/toys/utils/xdg.rb', line 107 def state_home # Source available in the toys-core gem end |