Class: Libis::Tools::Config
- Includes:
- Singleton
- Defined in:
- lib/libis/tools/config.rb
Overview
The Config class is a convenience class for easy configuration maintenance, loading and saving. It supports code defaults, loading configurations from multiple YAML files containing ERB statements. The Config class follows the Singleton pattern and behaves like a Hash/OpenStruct/HashWithIndifferentAccess. It also initializes a default Logger instance. The class also stores a system-wide Logger instance that will be used by Logger.
The parameters can be accessed by getter/setter method or using the Hash syntax:
require 'libis/tools/config'
cfg = ::Libis::Tools::Config
cfg['my_value'] = 10
p cfg.instance.my_value # => 10
cfg.instance.my_text = 'abc'
p cfg[:my_text] # => 'abc'
p cfg.logger.warn('message') # => W, [2015-03-16T12:51:01.180548 #28935] WARN -- : message
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#sources ⇒ Object
Returns the value of attribute sources.
Instance Method Summary collapse
-
#<<(file_or_hash) ⇒ Object
Load configuration parameters from a YAML file or Hash.
-
#clear! ⇒ Object
Clear all data.
- #method_missing(name, *args, &block) ⇒ Object
-
#reload ⇒ Object
Load all files and Hashes again.
-
#reload! ⇒ Object
Clear data and load all files and Hashes again.
-
#set_log_formatter(formatter = nil) ⇒ Object
Set the ::Logger instance’s formatter.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
43 44 45 46 |
# File 'lib/libis/tools/config.rb', line 43 def method_missing(name, *args, &block) result = config.send(name, *args, &block) self === config ? self : result end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
105 106 107 |
# File 'lib/libis/tools/config.rb', line 105 def config @config end |
#logger ⇒ Object
Returns the value of attribute logger.
105 106 107 |
# File 'lib/libis/tools/config.rb', line 105 def logger @logger end |
#sources ⇒ Object
Returns the value of attribute sources.
105 106 107 |
# File 'lib/libis/tools/config.rb', line 105 def sources @sources end |
Instance Method Details
#<<(file_or_hash) ⇒ Object
Load configuration parameters from a YAML file or Hash.
The file paths and Hashes are memorised and loaded again by the #reload methods.
52 53 54 55 |
# File 'lib/libis/tools/config.rb', line 52 def <<(file_or_hash) @config.send('<<', (file_or_hash)) { |data| @sources << data } self end |
#clear! ⇒ Object
Clear all data.
Not only all configuration parameters are deleted, but also the memorized list of loaded files and hashes are cleared and the logger configuration is reset to it’s default status.
82 83 84 85 86 87 88 |
# File 'lib/libis/tools/config.rb', line 82 def clear! @config.clear! @sources = Array.new @logger = ::Logger.new(STDOUT) set_log_formatter self end |
#reload ⇒ Object
Load all files and Hashes again.
Will not reset the configuration parameters. Parameters set directly on the configuration are kept intact unless they also exist in the files or hashes in which case they will be overwritten.
61 62 63 64 65 66 |
# File 'lib/libis/tools/config.rb', line 61 def reload sources = @sources.dup @sources.clear sources.each { |f| self << f } self end |
#reload! ⇒ Object
Clear data and load all files and Hashes again.
All configuration parameters are first deleted which means that any parameters added directly (not via file or hash) will no longer be available. Parameters set explicitly that also exist in the files or hashes will be reset to the values in those files and hashes.
73 74 75 76 |
# File 'lib/libis/tools/config.rb', line 73 def reload! @config.clear! reload end |
#set_log_formatter(formatter = nil) ⇒ Object
Set the ::Logger instance’s formatter. If the supplied formatter is missing or nil, a default formatter will be applied. The default formatter prints log lines like this:
<first char of severity>, [<timestamp>#<process-id>] <severity> -- <program_name> : <message>
97 98 99 100 101 102 103 |
# File 'lib/libis/tools/config.rb', line 97 def set_log_formatter(formatter = nil) self.logger.formatter = formatter || proc do |severity, time, progname, msg| "%s, [%s#%d] %5s -- %s: %s\n" % [severity[0..0], (time.strftime('%Y-%m-%dT%H:%M:%S.') << '%06d ' % time.usec), $$, severity, progname, msg] end end |