Class: FeduxOrgStdlib::DirectoryFinder

Inherits:
Object
  • Object
show all
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:

  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

  • 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:

Examples:

Create config with one writer and reader

module MyApplication
  class ClientDirectory < DirectoryFinder
  end
end

Defined Under Namespace

Modules: Exceptions

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#directoryDirectoryFinder (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

Parameters:

  • file (String)

    Path where config file is stored. The file will be read by the ‘config_engine`.

Returns:

  • (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.

Raises:

  • (Exceptions::DirectoryFileNotReadable)

    If an avaiable config file could not be read by the config engine



63
64
65
# File 'lib/fedux_org_stdlib/directory_finder.rb', line 63

def directory
  @directory
end

#loggerDirectoryFinder (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

Parameters:

  • file (String)

    Path where config file is stored. The file will be read by the ‘config_engine`.

Returns:

  • (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.

Raises:

  • (Exceptions::DirectoryFileNotReadable)

    If an avaiable config file could not be read by the config engine



63
64
65
# File 'lib/fedux_org_stdlib/directory_finder.rb', line 63

def logger
  @logger
end

#working_directoryDirectoryFinder (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

Parameters:

  • file (String)

    Path where config file is stored. The file will be read by the ‘config_engine`.

Returns:

  • (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.

Raises:

  • (Exceptions::DirectoryFileNotReadable)

    If an avaiable config file could not be read by the config engine



63
64
65
# File 'lib/fedux_org_stdlib/directory_finder.rb', line 63

def working_directory
  @working_directory
end

Instance Method Details

#preferred_directoryString

Return the path to the preferred config file

Returns:

  • (String)

    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