Class: FeduxOrgStdlib::DirectoryFinder
- Inherits:
-
Object
- Object
- FeduxOrgStdlib::DirectoryFinder
- Defined in:
- lib/fedux_org_stdlib/directory_finder.rb,
lib/fedux_org_stdlib/directory_finder/exceptions.rb
Overview
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:
Defined Under Namespace
Modules: Exceptions
Instance Attribute Summary collapse
-
#directory ⇒ DirectoryFinder
readonly
Create a new instance of config.
-
#logger ⇒ DirectoryFinder
readonly
Create a new instance of config.
-
#working_directory ⇒ DirectoryFinder
readonly
Create a new instance of config.
Instance Method Summary collapse
-
#initialize(directory: nil, working_directory: Dir.getwd, logger: FeduxOrgStdlib::Logging::Logger.new) ⇒ DirectoryFinder
constructor
A new instance of DirectoryFinder.
-
#preferred_directory ⇒ String
Return the path to the preferred config file.
Constructor Details
#initialize(directory: nil, working_directory: Dir.getwd, logger: FeduxOrgStdlib::Logging::Logger.new) ⇒ DirectoryFinder
Returns a new instance of DirectoryFinder.
65 66 67 68 69 70 71 72 73 74 75 |
# 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 |
Instance Attribute Details
#directory ⇒ DirectoryFinder (readonly)
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
63 64 65 |
# File 'lib/fedux_org_stdlib/directory_finder.rb', line 63 def directory @directory end |
#logger ⇒ DirectoryFinder (readonly)
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
63 64 65 |
# File 'lib/fedux_org_stdlib/directory_finder.rb', line 63 def logger @logger end |
#working_directory ⇒ DirectoryFinder (readonly)
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
63 64 65 |
# File 'lib/fedux_org_stdlib/directory_finder.rb', line 63 def working_directory @working_directory end |
Instance Method Details
#preferred_directory ⇒ String
Return the path to the preferred config file
80 81 82 |
# File 'lib/fedux_org_stdlib/directory_finder.rb', line 80 def preferred_directory _allowed_directory_paths.first end |