class RemiLib::Config::Resolver

Overview

The Resolver class is used to locate configuration files and user-specific data files in a platform-agnostic way.

Defined in:

remilib/config/resolver.cr

Class Method Summary

Instance Method Summary

Class Method Detail

def self.custom(baseName : String, configDir : String | Path, dataDir : String | Path, *, dontCreateMissingDirs : Bool = false) #

Creates a new Resolver that uses custom directories. When this is used, the #configDir and #dataDir are not generated based on the #baseName, and are instead the literal values passed into this method.

If dontCreateMissingDirs is true, then the #configDir and #dataDir are not created automatically.


[View source]
def self.windows(baseName : String, *, dontCreateMissingDirs : Bool = false) #

Creates a new Resolver that understands Windows %APPDATA%. If dontCreateMissingDirs is true, then the #configDir and #dataDir are not created automatically.


[View source]
def self.xdg(baseName : String, *, dontCreateMissingDirs : Bool = false) #

Creates a new Resolver that understands the XDG Base Directory Specification. If dontCreateMissingDirs is true, then the #configDir and #dataDir are not created automatically.


[View source]

Instance Method Detail

def baseName : String #

The directory name to use when storing configuration or data files. For example, if the #type is DirectoryType::Xdg, and the #baseName is equal to "test-program", then this Resolver will put configuration files under $XDG_CONFIG_HOME/test-program/ and data files under $XDG_DATA_HOME/some-program/.


[View source]
def configDir : Path #

The associated data directory. This is determined automatically based on the #baseName. For example, if the #type is DirectoryType::Xdg, and the #baseName is equal to "test-program", then this Resolver will put configuration files under $XDG_CONFIG_HOME/test-program/.


[View source]
def dataDir : Path #

The associated data directory. This is determined automatically based on the #baseName. For example, if the #type is DirectoryType::Xdg, and the #baseName is equal to "test-program", then this Resolver will put data files under $XDG_DATA_HOME/some-program/.


[View source]
def dataFile(symname : Symbol) : Path #

Given a symbolic name for a data file, return the Path associated with it. If the symbol is not registered with this Resolver, then this raises an UnknownDataFileError.


[View source]
def dataFile(symname : Symbol, &) : Nil #

Looks up the associated Path for a symbolic name for a data file, then opens the file using File.open and yields the stream. If the symbol is not registered with this Resolver, then this raises an UnknownDataFileError.


[View source]
def dataFile?(symname : Symbol) : Path | Nil #

Given a symbolic name for a data file, return the Path associated with it. If the symbol is not registered with this Resolver, this returns nil.


[View source]
def dataFile?(symname : Symbol, &) : Bool #

Looks up the associated Path for a symbolic name for a data file, then checks to see if the file exists. If it does, this opens the file using File.open and yields the stream, then returns true. Otherwise this returns false.

If the symbol is not registered with this Resolver, then this returns false.


[View source]
def defineDataFile(symname : Symbol, filename : String | Path) #

Registers a new data file with this Resolver such that a filename becomes associated with a Symbol. This symbol can later be used to reference that file.

The symbol must be unique to this Resolver instance.


[View source]
def defineDataFile!(symname : Symbol, filename : String | Path) #

Registers a new data file with this Resolver such that a filename becomes associated with a Symbol. This symbol can later be used to reference that file.

If the symbol is not unique to this Resolver instance, then the old path is forgotten and the symbol becomes associated with the new path.


[View source]
def recreateMissing #

Creates the #configDir and/or #dataDir if either are missing.


[View source]
def type : DirectoryType #

The associated type of configuration directory.


[View source]