class Liberic::Config

Provides an interface to ERiC's configuration parameters and logging.

Scope

This class is not meant to be instantiated directly. An instance is created the first time it is accessed in the Liberic namespace.

Liberic.config # will return an instance of Config

Configuration parameters

Configuration parameters can be accessed with the hash notation. Keys are passed to ERiC directly, so they must be in German (see ERiC documentation for this).

Example:

Liberic.config['validieren.fehler_max'] # 30

Config provides an english abstraction to these parameters with attr_accessor like getter/setter methods:

Liberic.config.validation_error_limit = 30

The most common settings are (german names in paranthesis)

Attributes

logger[R]

Public Class Methods

new() click to toggle source
# File lib/liberic/config.rb, line 38
def initialize
  install_logger
end

Public Instance Methods

[](key) click to toggle source
# File lib/liberic/config.rb, line 95
def [](key)
  convert_to_ruby(
    Helpers::Invocation.with_result_buffer do |handle|
      SDK::API.einstellung_lesen(key, handle)
    end
  )
end
[]=(key, value) click to toggle source
# File lib/liberic/config.rb, line 103
def []=(key, value)
  Helpers::Invocation.raise_on_error(
    SDK::API.einstellung_setzen(
      key,
      convert_to_eric(value)
    )
  )
end
logger=(logger) click to toggle source

Assign an instance of Logger that will be called whenever ERiC creates log output. Please note that ERiC's mechanism to write log files to disk is disabled as soon as a Config class is instantiated!

Attributes

  • logger - A Ruby Logger instance or nil

Examples

An instance of Config is provided in the Liberic namespace. A logger can be assigned like this:

Liberic.config.logger = Logger.new(STDOUT)
Liberic.config.logger.level = Logger::WARN

ERiC does not send debug log messages by default. To do this, change ERiC's configuration:

Liberic.config.detailed_logs = true

and make sure :level: of your Logger is not filtering debug messages.

# File lib/liberic/config.rb, line 66
def logger=(logger)
  @logger = logger
end

Private Instance Methods

convert_to_eric(value) click to toggle source
# File lib/liberic/config.rb, line 138
def convert_to_eric(value)
  if [true, false].include?(value)
    value ? 'ja' : 'nein'
  else
    value
  end.to_s.encode(SDK::Configuration::ENCODING)
end
convert_to_ruby(value) click to toggle source
# File lib/liberic/config.rb, line 126
def convert_to_ruby(value)
  if value.to_i.to_s == value
    value.to_i
  elsif ['ja', 'yes'].include?(value.downcase)
    true
  elsif ['nein', 'no'].include?(value.downcase)
    false
  else
    value.force_encoding(SDK::Configuration::ENCODING).encode(Encoding::default_external)
  end
end
get(key) click to toggle source
# File lib/liberic/config.rb, line 146
def get(key)
  Helpers::Invocation.with_result_buffer(true) do |handle|
    SDK::API.einstellung_lesen(key, handle)
  end
end
install_logger() click to toggle source
# File lib/liberic/config.rb, line 114
def install_logger
  Helpers::Invocation.raise_on_error(
    SDK::API.registriere_log_callback(
      SDK::API.generate_log_callback do |category, level, message|
        if @logger.respond_to?(:add)
          @logger.add(SDK::Types::LOGGER_SEVERITY[level], message, category)
        end
      end,
    0, nil)
  )
end
set(key, value) click to toggle source
# File lib/liberic/config.rb, line 152
def set(key, value)
  Helpers::Invocation.raise_on_error(SDK::API.einstellung_setzen(key, value))
  true
end