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:

  1. $HOME/.config/<application_name>/<config_directory>

  2. $HOME/.<application_name>/<config_directory>

  3. $HOME/.<config_directory>

  4. /etc/.<application_name>/<config_directory>

Please keep in mind

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:

If you want the class to look for your config file at a different place overwrite the following method

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

directory[R]

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.
logger[R]

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.
working_directory[R]

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

new( directory: nil, working_directory: Dir.getwd, logger: FeduxOrgStdlib::Logging::Logger.new ) click to toggle source
# 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

preferred_directory() click to toggle source

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

_allowed_directory_paths() click to toggle source

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
_application_name() click to toggle source

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
_available_directory() click to toggle source
# File lib/fedux_org_stdlib/directory_finder.rb, line 145
def _available_directory
  _allowed_directory_paths.find { |f| ::File.directory? f }
end
_class_name() click to toggle source
# File lib/fedux_org_stdlib/directory_finder.rb, line 137
def _class_name
  self.class.name.to_s.demodulize
end
_directory() click to toggle source

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
_module_name() click to toggle source
# File lib/fedux_org_stdlib/directory_finder.rb, line 141
def _module_name
  self.class.to_s.deconstantize
end