class Lorj::Config

Lorj::Config is a generic class for configuration management. It is used by lorj to get/set data

lorj uses following function in different context:

In your main:

In Process functions: The Config object is accessible as 'config'.

In Controller functions. Usually, the process has implemented everything. You should not use the config object. Thus, config object is not accessible.

Add Runtime/local functions

Public Class Methods

new(config_name = nil) click to toggle source

Load yaml documents (defaults + config) If config doesn't exist, it will be created, empty with 'defaults:' only

  • Args :

    • config_name : Config file name to use. By default, file path is

      built as #{PrcLib.data_path}/config.yaml
      
  • Returns : -

  • Raises :

    • ++ ->

# File lib/lorj_config.rb, line 110
def initialize(config_name = nil)
  config_layers = []

  # Application layer
  config_layers << define_default_layer

  # runtime Config layer
  config_layers << define_controller_data_layer

  # Local Config layer
  local = define_local_layer
  config_layers << local

  # runtime Config layer
  config_layers << define_runtime_layer

  if PrcLib.data_path.nil?
    PrcLib.fatal(1, 'Internal PrcLib.data_path was not set.')
  end

  initialize_local(local[:config], config_name)

  initialize_layers(config_layers)
end

Public Instance Methods

[](key, default = nil) click to toggle source

Call get function

  • Args :

    • key : key name

    • default: Default value to set if not found.

  • Returns : value found or default

  • Raises : nothing

# File lib/lorj_config.rb, line 247
def [](key, default = nil) # Re-define PRC::CoreConfig []= function
  return p_get(:keys => [key]) if exist?(key)
  default
end
config_dump(names = %w(local default)) click to toggle source

Basic dump

  • Args :

  • Returns :

    • hash of config hashes.

  • Raises : nothing

# File lib/lorj_config.rb, line 80
def config_dump(names = %w(local default))
  # Build a config hash.

  res = {}

  names = %w(local default) unless names.is_a?(Array)

  options = _common_options_get(:names => names)
  config_layers = options[0][0]
  if names.length == 1
    res = config_layers[0][:config].data
  else
    config_layers.each do |layer|
      res[layer[:name]] = layer[:config].data
    end
  end
  res
end
config_filename(name = 'local') click to toggle source

This function return the filename of the config layer asked:

  • Args :

  • layer_name : Layer name to get the config file name

# File lib/lorj_config.rb, line 64
def config_filename(name = 'local')
  index =  layer_index(name)

  index = 1 if index.nil?

  @config_layers[index][:config].filename
end
define_controller_data_layer() click to toggle source
# File lib/lorj_config.rb, line 148
def define_controller_data_layer
  PRC::CoreConfig.define_layer :name => 'controller'
end
define_default_layer() click to toggle source
# File lib/lorj_config.rb, line 135
def define_default_layer
  PRC::CoreConfig.define_layer(:name => 'default',
                               :config => Lorj.defaults,
                               :set => false, :load => true)
end
define_local_layer(latest_version = nil) click to toggle source
# File lib/lorj_config.rb, line 141
def define_local_layer(latest_version = nil)
  PRC::CoreConfig.define_layer(:name => 'local',
                               :config => \
                                PRC::SectionConfig.new(nil, latest_version),
                               :load => true, :save => true)
end
define_runtime_layer() click to toggle source
# File lib/lorj_config.rb, line 152
def define_runtime_layer
  PRC::CoreConfig.define_layer
end
fatal_if_inexistent(key) click to toggle source

Function to return in fatal error if a config data is nil. Help to control function requirement.

  • Args :

    • key : Symbol/String(converted to symbol) key name to test.

  • Returns : nothing

  • Raises :

    • fatal : Call to PrcLib.fatal to exit the application with error 1.

# File lib/lorj_config.rb, line 443
def fatal_if_inexistent(key)
  PrcLib.fatal(1, "Internal error - %s: '%s' is missing",
               caller, key) unless get(key)
end
get(key, default = nil) click to toggle source

Get function Will search over several places:

  • runtime - Call internal runtime_get -

  • local config (config>yaml) - Call internal local_get -

  • application default (defaults.yaml) - Call Lorj.defaults.get -

  • default

key can be an array, a string (converted to a symbol) or a symbol.

  • Args :

    • key : key name

    • default: Default value to set if not found.

  • Returns : value found or default

  • Raises : nothing

# File lib/lorj_config.rb, line 234
def get(key, default = nil)
  self[key, default]
end
get_section(key) click to toggle source

get_section helps to identify in which section the data is defined by data model of the application.

  • Args :

    • key : key name

  • Returns :

    • the section name found

    OR

    • nil

  • Raises : nothing

# File lib/lorj_config.rb, line 265
def get_section(key)
  section = Lorj.defaults.get_meta_section(key)

  unless section
    return PrcLib.debug('%s: Unable to get account data '\
                        "'%s'. No section found. check defaults.yaml.",
                        __callee__, key)
  end
  section
end
initialize_local(config, config_name = nil) click to toggle source
# File lib/lorj_config.rb, line 156
def initialize_local(config, config_name = nil)
  config_name = initialize_local_filename(config_name)

  PrcLib.ensure_dir_exists(File.dirname(config_name))

  if File.exist?(config_name)
    config.load(config_name)

    if config.data.rh_key_to_symbol?(2)
      config.rh_key_to_symbol(2)
      config.save config_name
    end

  else
    config.data[:default] =  nil
    # Write the empty file
    PrcLib.info('Creating your default configuration file ...')
    config.save config_name
  end
end
initialize_local_filename(config_name = nil) click to toggle source
# File lib/lorj_config.rb, line 177
def initialize_local_filename(config_name = nil)
  config_default_name = 'config.yaml'

  config_name = nil unless config_name.is_a?(String)
  if config_name
    if File.dirname(config_name) == '.'
      config_name = File.join(PrcLib.data_path, config_name)
    end
    config_name = File.expand_path(config_name)
    unless File.exist?(config_name)
      PrcLib.warning("Config file '%s' doesn't exists. Using default one.",
                     config_name)
      config_name = File.join(PrcLib.data_path, config_default_name)
    end
  else
    config_name = File.join(PrcLib.data_path, config_default_name)
  end
  config_name
end
local_default_exist?(key) click to toggle source

Function to check default keys existence(in section :default) from local config file only.

  • Args :

    • key : Symbol/String(converted to symbol) key name to test.

  • Returns : -

  • Raises : nothing

# File lib/lorj_config.rb, line 344
def local_default_exist?(key)
  local_exist?(key)
end
local_del(key, section = :default) click to toggle source

Function to Delete a key value in local config file only.

  • Args :

    • key : Symbol/String(converted to symbol) key name to test.

    • section : Section name to test the key.

  • Returns :

    • value deleted

    OR

    • false

  • Raises : nothing

# File lib/lorj_config.rb, line 422
def local_del(key, section = :default)
  key = key.to_sym if key.class == String

  return false if key.nil?

  index = layer_index('local')
  config = @config_layers[index][:config]
  config.data_options(:section => section)
  config.del(key)
end
local_exist?(key, section = :default) click to toggle source

Function to check key existence from local config file only.

  • Args :

    • key : Symbol/String(converted to symbol) key name to test.

    • section : Section name to test the key.

  • Returns : -

  • Raises : nothing

# File lib/lorj_config.rb, line 357
def local_exist?(key, section = :default)
  index = layer_index('local')

  config = @config_layers[index][:config]
  config.data_options(:section => section)

  config.exist?(key)
end
local_get(key, section = :default, default = nil) click to toggle source

Function to Get a key value from local config file only.

  • Args :

    • key : Symbol/String(converted to symbol) key name to test.

    • section : Section name to test the key.

    • default : default value if not found.

  • Returns :

    • Value get or default.

  • Raises : nothing

# File lib/lorj_config.rb, line 399
def local_get(key, section = :default, default = nil)
  key = key.to_sym if key.class == String

  return default unless local_exist?(key, section)

  index = layer_index('local')
  config = @config_layers[index][:config]
  config.data_options(:section => section)
  config[key]
end
local_set(key, value, section = :default) click to toggle source

Function to set a key value in local config file only.

  • Args :

    • key : Symbol/String(converted to symbol) key name to test.

    • value : Value to set

    • section : Section name to test the key.

  • Returns :

    • Value set.

  • Raises : nothing

# File lib/lorj_config.rb, line 377
def local_set(key, value, section = :default)
  key = key.to_sym if key.class == String
  return false if !key || !value

  index = layer_index('local')

  config = @config_layers[index][:config]
  config.data_options(:section => section)
  config[key] = value
end
meta_each() { |section, key, value| ... } click to toggle source

each loop on Application Account section/key (meta data). This loop will extract data from :section of the application definition (defaults.yaml) key identified as account exclusive (:account_exclusive = true) are not selected.

  • Args :

    • ++ ->

  • Returns : -

  • Raises :

    • ++ ->

# File lib/lorj_config.rb, line 460
def meta_each
  Lorj.defaults.meta_each do |section, key, value|
    next if !value.nil? && value.rh_get(:account_exclusive).is_a?(TrueClass)
    yield section, key, value
  end
end
runtime_exist?(key) click to toggle source

Check if the key exist as a runtime data.

  • Args :

    • key : key name. It do not support it to be a key tree

      (Arrays of keys).
  • Returns :

    • true/false

  • Raises : Nothing

# File lib/lorj_config.rb, line 288
def runtime_exist?(key)
  index = layer_index('runtime')
  @config_layers[index][:config].exist?(key)
end
runtime_get(key) click to toggle source

Get exclusively the Runtime data. Internally used by get.

  • Args :

    • key : key name. It do not support it to be a key tree

      (Arrays of keys).
  • Returns :

    • key value.

  • Raises : Nothing

# File lib/lorj_config.rb, line 303
def runtime_get(key)
  index = layer_index('runtime')
  @config_layers[index][:config][key]
end
save_local_config() click to toggle source

Save the config.yaml file.

  • Args : nothing

  • Returns :

    • true/false

  • Raises : nothing

# File lib/lorj_config.rb, line 316
def save_local_config
  index = layer_index('local')
  file = file(nil, :index => index)
  begin
    result = save(:index => index)
  rescue => e
    PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
    return false
  end

  if result
    PrcLib.info('Configuration file "%s" updated.', file)
    return true
  end
  PrcLib.debug('Configuration file "%s" was NOT updated.', file)
  false
end
set(key, value, options = {}) click to toggle source

Function to set a runtime key/value, but remove it if value is nil. To set in config.yaml, use Lorj::Config::local_set To set on extra data, like account information, use Lorj::Config::extra_set

  • Args :

    • key : key name. Can be an key tree (Array of keys).

    • value : Value to set

    • options : possible options:

      • :name : layer to exclusively set data.

      • :index : layer index to exclusively set data. If neither :name or :index is set, set will use the 'runtime' layer.

  • Returns :

    • value set

  • Raises : Nothing

# File lib/lorj_config.rb, line 214
def set(key, value, options = {})
  p_set(options.merge(:keys => [key], :value => value))
end