Class: FeduxOrgStdlib::AppConfig
- Inherits:
-
Object
- Object
- FeduxOrgStdlib::AppConfig
- Defined in:
- lib/fedux_org_stdlib/app_config.rb,
lib/fedux_org_stdlib/app_config/exceptions.rb
Overview
This class makes a config file available as an object. The config file needs to be ‘YAML` by default. It is read by `Psych` and converted to a hash. If you chose to use a different file format: Each config file needs to translatable to a hash or another data structure which responds to `[]` by the given `config_engine`. If no suitable config file can be found the config uses only the defined defaults within the class.
By default it will look for a suitable config file in the given order:
-
$HOME/.config/<application_name>/<config_file>.yaml
-
$HOME/.<application_name>/<config_file>.yaml
-
$HOME/.<config_file>.yaml
-
$HOME/.<config_file>rc
-
/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 “Config” strip off, e.g “ClientConfig” becomes “clients.yaml” (mind the pluralized name)
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:
Defined Under Namespace
Modules: Exceptions
Class Method Summary collapse
- .known_options ⇒ Object private
-
.option(option, default_value) ⇒ Object
Define a writer and a reader for option.
-
.option_reader(option, default_value) ⇒ Object
Define a reader for option.
-
.option_writer(option) ⇒ Object
private
Define a writer for option.
-
.process_environment ⇒ Object
Get access to process environment.
Instance Method Summary collapse
-
#initialize(file: _available_config_file, config_engine: Psych, logger: FeduxOrgStdlib::Logging::Logger.new, check_unknown_options: true) ⇒ AppConfig
constructor
Create a new instance of config.
-
#known_options ⇒ Object
Show known options for configuration.
-
#lock ⇒ Object
Lock the configuration.
-
#preferred_configuration_file ⇒ String
Return the path to the preferred configuration file.
-
#to_s ⇒ String
Output a string presentation of the configuration.
Constructor Details
#initialize(file: _available_config_file, config_engine: Psych, logger: FeduxOrgStdlib::Logging::Logger.new, check_unknown_options: true) ⇒ AppConfig
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
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 183 def initialize( file: _available_config_file, config_engine: Psych, logger: FeduxOrgStdlib::Logging::Logger.new, check_unknown_options: true ) @logger = logger unless file logger.debug "No configuration file found at #{_allowed_config_file_paths.to_list}, therefor I'm going to use an empty config object instead." @__config = {} return end begin yaml = config_engine.safe_load(File.read(file), [Symbol]) rescue StandardError => e raise Exceptions::ConfigFileNotReadable, JSON.dump(message: e., file: file) end if yaml.blank? @__config = {} elsif yaml.is_a? Hash yaml = yaml.deep_symbolize_keys = yaml.deep_symbolize_keys.slice(*) = yaml.keys - .keys logger.warn "Unknown config options #{(unknown_options).to_list} in config file #{file} detected. Please define them in your config class or remove the entries in your config file or disable check via `check_unknown_options: false` to get rid of this warning." unless .blank? && == true @__config = Hash() else logger.warn "There seems to be a problem transforming config file \"#{file}\" to a hash, therefor I will use an empty config object." @__config = {} end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object (private)
357 358 359 360 361 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 357 def method_missing(*args, &block) $stderr.puts "Please check if you have defined an option for #{args.first}." super end |
Class Method Details
.known_options ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
74 75 76 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 74 def ||= Set.new end |
.option(option, default_value) ⇒ Object
Define a writer and a reader for option
143 144 145 146 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 143 def option(option, default_value) option_reader(option, default_value) option_writer(option) end |
.option_reader(option, default_value) ⇒ Object
Define a reader for option
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 97 def option_reader(option, default_value) option = option.to_sym fail Exceptions::OptionNameForbidden, JSON.dump(option: option) if _reserved_key_words.include? option define_method option do _config.fetch(option, default_value) end << option end |
.option_writer(option) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Define a writer for option
Please make sure that you define a reader as well. Otherwise you cannot access the option. Under normal conditions it does not make sense to use this method.
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 122 def option_writer(option) fail Exceptions::OptionNameForbidden, JSON.dump(option: option) if _reserved_key_words.include? "#{option}=".to_sym define_method "#{option}=".to_sym do |value| begin _config[option.to_sym] = value rescue RuntimeError raise Exceptions::ConfigLocked end end << option end |
.process_environment ⇒ Object
Get access to process environment
This might be handy to define default options
86 87 88 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 86 def process_environment @process_environment ||= ProcessEnvironment.new end |
Instance Method Details
#known_options ⇒ Object
Show known options for configuration
222 223 224 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 222 def self.class. end |
#lock ⇒ Object
Lock the configuration
227 228 229 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 227 def lock _config.freeze end |
#preferred_configuration_file ⇒ String
Return the path to the preferred configuration file
265 266 267 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 265 def preferred_configuration_file _allowed_config_file_paths.first end |
#to_s ⇒ String
Output a string presentation of the configuration
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 235 def to_s # header 'length' = 6 letters length = self.class..reduce(6) { |a, e| e.size > a ? e.size : a } result = [] result << format("%#{length}s | %s", 'option', 'value') result << format('%s + %s', '-' * length, '-' * 80) self.class..each do |o| value = public_send(o) value = if value == false Array(value) elsif value.blank? Array('is undefined') elsif value.is_a?(Hash) || value.is_a?(Array) value else Array(value) end result << format("%#{length}s | %s", o, value.to_list) end result.join("\n") end |