class FeduxOrgStdlib::DirectoryFinder
This class detects the file name for an config file. By default it will look for a suitable config file in the given order:
-
$HOME/.config/<application_name>/<config_directory>
-
$HOME/.<application_name>/<config_directory>
-
$HOME/.<config_directory>
-
/etc/.<application_name>/<config_directory>
Please keep in mind
-
application_name: Module of your class, e.g. “MyApplication” becomes “my_application”
-
config_directory: Name of your class and “Directory” strip off, e.g “ClientDirectory” becomes “client”
Most conventions defined by me are implemented as separate methods. If one convention is not suitable for your use case, just overwrite the method.
If you prefer to use a different path to the config file or name of the config file one of the following methods needs to be overwritten:
-
config_directory
-
config_name
-
application_name
If you want the class to look for your config file at a different place overwrite the following method
-
allowed_directory_paths
Below you find some examples for the usage of the class:
@example Create config with one writer and reader
module MyApplication class ClientDirectory < DirectoryFinder end end
Attributes
Create a new instance of config
It tries to find a suitable directory. If it doesn't find one the config is empty and uses the defaults defined within a config class
@param [String] file
Path where config file is stored. The file will be read by the `config_engine`.
@raise [Exceptions::DirectoryFileNotReadable]
If an avaiable config file could not be read by the config engine
@return [DirectoryFinder]
The config instance. If the resulting data structure created by the config_engine does not respond to `:[]` an empty config object will be created.
Create a new instance of config
It tries to find a suitable directory. If it doesn't find one the config is empty and uses the defaults defined within a config class
@param [String] file
Path where config file is stored. The file will be read by the `config_engine`.
@raise [Exceptions::DirectoryFileNotReadable]
If an avaiable config file could not be read by the config engine
@return [DirectoryFinder]
The config instance. If the resulting data structure created by the config_engine does not respond to `:[]` an empty config object will be created.
Create a new instance of config
It tries to find a suitable directory. If it doesn't find one the config is empty and uses the defaults defined within a config class
@param [String] file
Path where config file is stored. The file will be read by the `config_engine`.
@raise [Exceptions::DirectoryFileNotReadable]
If an avaiable config file could not be read by the config engine
@return [DirectoryFinder]
The config instance. If the resulting data structure created by the config_engine does not respond to `:[]` an empty config object will be created.
Public Class Methods
# File lib/fedux_org_stdlib/directory_finder.rb, line 65 def initialize( directory: nil, working_directory: Dir.getwd, logger: FeduxOrgStdlib::Logging::Logger.new ) @logger = logger @working_directory = working_directory @directory ||= (directory || _available_directory) logger.debug "No config directory found at #{_allowed_directory_paths.to_list}, therefor I'm going to use an empty config object instead." unless directory end
Public Instance Methods
Return the path to the preferred config file @return [String]
The path to the preferred config file
# File lib/fedux_org_stdlib/directory_finder.rb, line 80 def preferred_directory _allowed_directory_paths.first end
Private Instance Methods
The paths where to look for the config file
@return [Array]
A list of paths where the config object should look for its config file.
# File lib/fedux_org_stdlib/directory_finder.rb, line 127 def _allowed_directory_paths [ ::File.expand_path(::File.join('~', '.config', _application_name, _directory)), ::File.expand_path(::File.join('~', format('.%s', _application_name), _directory)), ::File.expand_path(::File.join('~', format('.%s', _directory))), ::File.expand_path(::File.join('/etc', _application_name, _directory)), ::File.expand_path(::File.join(working_directory, _directory)) ] end
The name of your application
@return [String]
This will strip of the class part of fully qualified class name and converted it to a path.
@example Determine application name
class MyApplication::MyDirectory; end
This will be converted to
my_application
# File lib/fedux_org_stdlib/directory_finder.rb, line 118 def _application_name _module_name.underscore end
# File lib/fedux_org_stdlib/directory_finder.rb, line 145 def _available_directory _allowed_directory_paths.find { |f| ::File.directory? f } end
# File lib/fedux_org_stdlib/directory_finder.rb, line 137 def _class_name self.class.name.to_s.demodulize end
The base name of the config
@return [String]
This one returns the base name of the config file (without the file extension). It uses the class name of the config class
@example Determine the base name of the config
class ClientDirectory; end
This will result in `client` as base name for the config file.
# File lib/fedux_org_stdlib/directory_finder.rb, line 97 def _directory unless (name = _class_name.sub(/Directory/, '').underscore).blank? return name end fail Exceptions::ClassNameIsMissing, JSON.dump(klass: _class_name) end
# File lib/fedux_org_stdlib/directory_finder.rb, line 141 def _module_name self.class.to_s.deconstantize end