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
Instance Attribute Summary collapse
-
#check_unknown_options ⇒ AppConfig
readonly
Create a new instance of config.
-
#config_engine ⇒ AppConfig
readonly
Create a new instance of config.
-
#files ⇒ AppConfig
readonly
Create a new instance of config.
-
#logger ⇒ AppConfig
readonly
Create a new instance of config.
-
#merge_files ⇒ AppConfig
readonly
Create a new instance of config.
-
#safe ⇒ AppConfig
readonly
Create a new instance of config.
-
#working_directory ⇒ AppConfig
readonly
Create a new instance of config.
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
-
#clear ⇒ Object
Clear configuration.
-
#defaults ⇒ Object
Return configuration resetted to defaults.
-
#initialize(file: nil, merge_files: false, config_engine: Psych, logger: FeduxOrgStdlib::Logging::Logger.new, check_unknown_options: true, working_directory: Dir.getwd, safe: true) ⇒ AppConfig
constructor
A new instance of AppConfig.
-
#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.
-
#redetect ⇒ Object
Redected configuration file and reload config afterwards.
-
#reload ⇒ Object
Reload from already found config file.
- #to_h(keys: [], remove_blank: false) ⇒ Object
-
#to_s ⇒ String
Output a string presentation of the configuration.
-
#to_yaml(prepend: nil, **args) ⇒ Object
Convert config to yaml.
Constructor Details
#initialize(file: nil, merge_files: false, config_engine: Psych, logger: FeduxOrgStdlib::Logging::Logger.new, check_unknown_options: true, working_directory: Dir.getwd, safe: true) ⇒ AppConfig
Returns a new instance of AppConfig.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 186 def initialize( file: nil, merge_files: false, config_engine: Psych, logger: FeduxOrgStdlib::Logging::Logger.new, check_unknown_options: true, working_directory: Dir.getwd, safe: true ) @logger = logger @files = Array(file) @merge_files = merge_files @config_engine = config_engine = @working_directory = working_directory @safe = safe detect_files if @files.blank? load_config end |
Instance Attribute Details
#check_unknown_options ⇒ AppConfig (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
184 185 186 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 184 def end |
#config_engine ⇒ AppConfig (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
184 185 186 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 184 def config_engine @config_engine end |
#files ⇒ AppConfig (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
184 185 186 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 184 def files @files end |
#logger ⇒ AppConfig (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
184 185 186 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 184 def logger @logger end |
#merge_files ⇒ AppConfig (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
184 185 186 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 184 def merge_files @merge_files end |
#safe ⇒ AppConfig (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
184 185 186 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 184 def safe @safe end |
#working_directory ⇒ AppConfig (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
184 185 186 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 184 def working_directory @working_directory 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
#clear ⇒ Object
Clear configuration
267 268 269 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 267 def clear @__config = {} end |
#defaults ⇒ Object
Return configuration resetted to defaults
272 273 274 275 276 277 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 272 def defaults config = dup config.clear config end |
#known_options ⇒ Object
Show known options for configuration
219 220 221 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 219 def self.class. end |
#lock ⇒ Object
Lock the configuration
224 225 226 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 224 def lock _config.freeze end |
#preferred_configuration_file ⇒ String
Return the path to the preferred configuration file
262 263 264 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 262 def preferred_configuration_file _allowed_config_file_paths.first end |
#redetect ⇒ Object
Redected configuration file and reload config afterwards
213 214 215 216 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 213 def redetect detect_files load_config end |
#reload ⇒ Object
Reload from already found config file
208 209 210 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 208 def reload load_config end |
#to_h(keys: [], remove_blank: false) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 279 def to_h(keys: [], remove_blank: false) = if keys.blank? .to_a else .to_a & keys.map(&:to_sym) end .sort.each_with_object({}) do |e, a| next if remove_blank && public_send(e).blank? a[e] = public_send(e) end end |
#to_s ⇒ String
Output a string presentation of the configuration
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 232 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..sort.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 |
#to_yaml(prepend: nil, **args) ⇒ Object
Convert config to yaml
293 294 295 296 297 298 299 |
# File 'lib/fedux_org_stdlib/app_config.rb', line 293 def to_yaml(prepend: nil, **args) yaml = Psych.dump to_h(**args).deep_stringify_keys return yaml.split("\n").map { |l| l.prepend prepend }.join("\n") if prepend yaml end |