Class: Libis::Tools::Config

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/libis/tools/config.rb

Overview

The Singleton Config class is a convenience class for easy configuration maintenance, loading and saving. It also initializes a default logger and supports creating extra loggers. The logging infrastructure is based on the ::Logging gem and supports the Logger class.

For the configuration parameters, it supports code defaults, loading configurations from multiple YAML files containing ERB statements. The Config class behaves like a Hash/OpenStruct/HashWithIndifferentAccess.

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 #123.456]  WARN : message

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object

Instance method that allows to access the configuration parameters by method.



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

def method_missing(name, *args, **kwargs, &block)
  result = config.send(name, *args, **kwargs, &block)
  self === config ? self : result
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



124
125
126
# File 'lib/libis/tools/config.rb', line 124

def config
  @config
end

#sourcesObject

Returns the value of attribute sources.



124
125
126
# File 'lib/libis/tools/config.rb', line 124

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.

Parameters:



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

def <<(file_or_hash)
  sync do
    @config.send('<<', (file_or_hash)) { |data| @sources << data }
    self
  end
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.



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

def clear!
  sync do
    @config.clear!
    @sources = Array.new
    self.logger
    self
  end
end

#get_log_formatterObject

Gets the default ::Logging formatter.

This in an instance of a layout that prints in the default message format.

The default layout prints log lines like this:

<first char of severity>, [<timestamp> #<process-id>.<thread-id] <severity> : <message>


108
109
110
111
# File 'lib/libis/tools/config.rb', line 108

def get_log_formatter
  # noinspection RubyResolve
  ::Logging::Layouts::Pattern.new(DEFAULT_LOG_LAYOUT_PARAMETERS)
end

#logger(name = nil, appenders = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/libis/tools/config.rb', line 113

def logger(name = nil, appenders = nil)
  sync do
    name ||= 'root'
    logger = ::Logging.logger[name]
    if logger.appenders.empty?
      logger.appenders = appenders || ::Logging.appenders.stdout(layout: get_log_formatter)
    end
    logger
  end
end

#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.



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

def reload
  sync do
    sources = @sources.dup
    @sources.clear
    sources.each { |f| self << f }
    self
  end
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.



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

def reload!
  sync do
    @config.clear!
    reload
  end
end