class FeduxOrgStdlib::FileFinder

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

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 ClientFile < FileFinder
  end
end

Attributes

file[R]

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

@param [String] file

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

@raise [Exceptions::FileFinderNotReadable]

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

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

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

@param [String] file

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

@raise [Exceptions::FileFinderNotReadable]

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

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

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

@param [String] file

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

@raise [Exceptions::FileFinderNotReadable]

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

@return [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.

Public Class Methods

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

Public Instance Methods

preferred_configuration_file() click to toggle source

Return the path to the preferred configuration file @return [String]

The path to the preferred configuration file
# File lib/fedux_org_stdlib/file_finder.rb, line 81
def preferred_configuration_file
  _allowed_config_file_paths.first
end

Private Instance Methods

_allowed_config_file_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/file_finder.rb, line 146
def _allowed_config_file_paths
  [
    ::File.expand_path(::File.join('~', '.config', _application_name, _config_file)),
    ::File.expand_path(::File.join('~', format('.%s', _application_name), _config_file)),
    ::File.expand_path(::File.join('~', format('.%s', _config_file))),
    ::File.expand_path(::File.join('~', format('.%src', _config_name))),
    ::File.expand_path(::File.join('/etc', _application_name, _config_file)),
    ::File.expand_path(::File.join(working_directory, _config_file))
  ]
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::MyFile; end

This will be converted to

my_application
# File lib/fedux_org_stdlib/file_finder.rb, line 137
def _application_name
  _module_name.underscore
end
_available_config_file() click to toggle source
# File lib/fedux_org_stdlib/file_finder.rb, line 165
def _available_config_file
  _allowed_config_file_paths.find { |f| ::File.exist? f }
end
_class_name() click to toggle source
# File lib/fedux_org_stdlib/file_finder.rb, line 157
def _class_name
  self.class.name.to_s.demodulize
end
_config_file() click to toggle source

The name of the config file

@return [String]

The name of the config file. It defaults to `<config_name>.yaml`. If
you want to use a different file name you need to overwrite this
method.
# File lib/fedux_org_stdlib/file_finder.rb, line 93
def _config_file
  "#{_config_name}#{_config_file_suffix}"
end
_config_file_suffix() click to toggle source

The suffix of the config file

@return [String]

The suffix of the config file
# File lib/fedux_org_stdlib/file_finder.rb, line 101
def _config_file_suffix
  '.yaml'
end
_config_name() 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 ClientFile; end

This will result in `client` as base name for the config file.

# File lib/fedux_org_stdlib/file_finder.rb, line 116
def _config_name
  unless (name = _class_name.sub(/File/, '').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/file_finder.rb, line 161
def _module_name
  self.class.to_s.deconstantize
end