Class: FeduxOrgStdlib::FileFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/fedux_org_stdlib/file_finder.rb,
lib/fedux_org_stdlib/file_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_file>.yaml

  2. $HOME/.<application_name>/<config_file>.yaml

  3. $HOME/.<config_file>.yaml

  4. $HOME/.<config_file>rc

  5. /etc/.<application_name>/<config_file>.yaml

Please keep in mind

  • application_name: Module of your class, e.g. “MyApplication” becomes “my_application”

  • config_file: Pluarized name of your class and “File” strip off, e.g “ClientFile” becomes “client.yaml”

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_file

  • config_name

  • application_name

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

  • allowed_config_file_paths

Below you find some examples for the usage of the class:

Examples:

Create config with one writer and reader

module MyApplication
  class ClientFile < FileFinder
  end
end

Defined Under Namespace

Modules: Exceptions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file: nil, working_directory: Dir.getwd, logger: FeduxOrgStdlib::Logging::Logger.new) ⇒ FileFinder

Returns a new instance of FileFinder.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fedux_org_stdlib/file_finder.rb', line 66

def initialize(
  file: nil,
  working_directory: Dir.getwd,
  logger: FeduxOrgStdlib::Logging::Logger.new
)
  @logger            = logger
  @working_directory = working_directory
  @file              ||= (file || _available_config_file)

  logger.debug "None of this files exist #{_allowed_config_file_paths.to_list}." unless file
end

Instance Attribute Details

#fileFileFinder (readonly)

Create a new instance of config

It tries to find a suitable configuration file. 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:

  • (FileFinder)

    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::FileFinderNotReadable)

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



64
65
66
# File 'lib/fedux_org_stdlib/file_finder.rb', line 64

def file
  @file
end

#loggerFileFinder (readonly)

Create a new instance of config

It tries to find a suitable configuration file. 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:

  • (FileFinder)

    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::FileFinderNotReadable)

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



64
65
66
# File 'lib/fedux_org_stdlib/file_finder.rb', line 64

def logger
  @logger
end

#working_directoryFileFinder (readonly)

Create a new instance of config

It tries to find a suitable configuration file. 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:

  • (FileFinder)

    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::FileFinderNotReadable)

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



64
65
66
# File 'lib/fedux_org_stdlib/file_finder.rb', line 64

def working_directory
  @working_directory
end

Instance Method Details

#preferred_configuration_fileString

Return the path to the preferred configuration file

Returns:

  • (String)

    The path to the preferred configuration file



81
82
83
# File 'lib/fedux_org_stdlib/file_finder.rb', line 81

def preferred_configuration_file
  _allowed_config_file_paths.first
end