Class: Libis::Tools::Config

Inherits:
ConfigFile show all
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

Instance Method Summary collapse

Methods inherited from ConfigFile

#>>

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



101
102
103
# File 'lib/libis/tools/config.rb', line 101

def logger
  @logger
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.

Parameters:



47
48
49
# File 'lib/libis/tools/config.rb', line 47

def <<(file_or_hash)
  super(file_or_hash) { |data| @sources << data }
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.



78
79
80
81
82
83
84
# File 'lib/libis/tools/config.rb', line 78

def clear!
  super
  @sources = Array.new
  @logger = ::Logger.new(STDOUT)
  set_log_formatter
  self
end

#deep_struct_clear!Object



62
# File 'lib/libis/tools/config.rb', line 62

alias_method :deep_struct_clear!, :clear!

#reloadObject

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.



55
56
57
58
59
60
# File 'lib/libis/tools/config.rb', line 55

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.



69
70
71
72
# File 'lib/libis/tools/config.rb', line 69

def reload!
  deep_struct_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>

Parameters:

  • formatter (Proc) (defaults to: nil)

    the formatter procedure or nil for default formatter



93
94
95
96
97
98
99
# File 'lib/libis/tools/config.rb', line 93

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