class Lorj::MetaAppConfig

Private functions for MetaAppConfig

This class is the Meta Application configuration class accessible from PrcLib.metadata

A unique class instance load data from Lorj::Defaults thanks to the defaults.yaml. In the near future, sections `:setup` and `:sections` will be moved to a different file and loaded by this instance itself.

It implements Meta Config layers to help in loading default application meta data, and any controller/process redefinition.

The defaults.yaml :sections and :setup is defined as follow:

Public Class Methods

new(data) click to toggle source

Implements a 2 layers metadata config.

# File lib/lorj_meta.rb, line 387
def initialize(data)
  config_layers = []

  # Application layer
  config_layers << define_meta_app_layer(data)

  # mapping section/keys layer
  config_layers << define_meta_map_layer

  # controller Config layer
  config_layers << define_meta_controller_layer

  initialize_layers(config_layers)

  build_section_mapping
end

Public Instance Methods

auto_meta_exist?(data) click to toggle source

return 1st section/data existence.

If a key name is found in several different section,

auto_* functions, usually, will get the first section from a key/sections mapping Array except if you provide a '#' in the data name. (Ex: :'section1#key1')

The list of sections for one key is build thanks to build_section_mapping.

  • Args :

    • data : data name to check. Support 'section#data'.

  • Returns :

    • true/false

# File lib/lorj_meta.rb, line 471
def auto_meta_exist?(data)
  return nil unless data

  section, data = first_section(data)

  p_exist?(:keys => [:sections, section, data])
end
auto_section_data(data, *options) click to toggle source

Get model data options. Section name is determined by the associated data name

auto_* functions, usually, will get the first section from a key/sections mapping Array. But it supports also 'Section#Name' to determine the section to use instead of first one.

The list of sections for one key is build thanks to build_section_mapping.

  • Args :

    • data : data name. Support 'Section#Name'

    • options : Optionnal. List of sub keys in tree to get data.

  • Returns :

    • data options values

    OR

    • nil if:

      • missing data name as parameter.

      • data was not found. defined in /:sections/<section>/<data

# File lib/lorj_meta.rb, line 613
def auto_section_data(data, *options)
  return nil if data.nil?
  section, data = first_section(data)
  section_data(section, data, *options)
end
datas() click to toggle source

return the list of valid keys found in meta data.

  • Args :

  • Returns :

# File lib/lorj_meta.rb, line 525
def datas
  p_get(:keys => [:keys], :name => 'map').keys
end
define_controller_data(section, data, options, layer = 'controller') click to toggle source

Controller data definition which will enhance data Application definition. This function replace any controller definition. To update/add options to an existing controller data, use update_data. You can also use [], []=, etc… provided by parent class. This will work only if the 'controller' is the highest layer.

  • Args:

    • section : Symbol. Section name of the data to define.

    • data : Symbol. Name of the data

    • options : Hash. List of options

    • layer : optional. Layer name to define. All layers are authorized, except 'app'. 'app' is the protected application layer data. By default, the layer configured is 'controller'

# File lib/lorj_meta.rb, line 699
def define_controller_data(section, data, options, layer = 'controller')
  return nil unless check_par(section, Symbol,
                              data,    Symbol,
                              options, Hash,
                              layer,  [String, NilClass])

  keys = [:sections]
  keys << section << data

  layer = 'controller' if layer.nil? || layer == 'app'

  update_map(section, data)
  p_set(:keys => keys, :name => layer, :value => options)
end
del(type, section, data_keys, layer = 'controller') click to toggle source

section/data removal function

  • Args

    • section : Symbol. Section name of the data to define.

    • data : Symbol or Array of Symbols. Name of the data

    • layer : optional. Layer name to define. All layers are authorized, except 'app'. 'app' is the protected application layer data. By default, the layer configured is 'controller'

  • Returns

    • The value set or nil

# File lib/lorj_meta.rb, line 671
def del(type, section, data_keys, layer = 'controller')
  return nil unless check_par(type,      Symbol,
                              section,   Symbol,
                              data_keys, [Symbol, Array],
                              layer,     [String, NilClass])

  keys = build_keys(type, section, data_keys)

  layer = 'controller' if layer.nil? || %w(app map).include?(layer)

  delete_map(section, keys[2]) if keys[0] == :sections

  p_del(:keys => keys, :name => layer)
end
first_section(data) click to toggle source

return the 1st section name found of a data or the section discovered.

  • Args :

    • data : data name to search. It supports section#name.

  • Returns :

    • Array:

      • section name

      • key name

# File lib/lorj_meta.rb, line 488
def first_section(data)
  section, data = _detect_section(data, nil)
  return [section, data] unless section.nil? &&
                                p_exist?(:keys => [:keys, data])

  arr = p_get(:keys => [:keys, data])
  return nil unless arr.is_a?(Array) && arr[0]
  if /1\.8/ =~ RUBY_VERSION && arr.length > 1
    PrcLib.warning('Avoid using Lorj::MetaAppConfig.first_section(%s) with'\
                   ' Ruby 1.8 and searching for the first section as it do'\
                   ' not preserve the order of Hash keys.', data)
  end
  [arr[0], data]
end
layer_add(options) click to toggle source

Redefine CoreConfig#layer_add to add mapping build

See CoreConfig#layer_add for details

# File lib/lorj_meta.rb, line 407
def layer_add(options)
  p_layer_add(options)
  build_section_mapping
end
layer_remove(options) click to toggle source

Redefine CoreConfig#layer_remove to add mapping build

See CoreConfig#layer_remove for details

# File lib/lorj_meta.rb, line 415
def layer_remove(options)
  p_layer_remove(options)
  build_section_mapping
end
meta_each() { |section, key, value| ... } click to toggle source

Loop on Config metadata

  • Args :

    • code : Block of code on `section`, `key`, `value`

  • Returns :

    • nothing

# File lib/lorj_meta.rb, line 427
def meta_each
  data = p_get(:keys => [:sections], :merge => true)

  if data.nil?
    PrcLib.warning('No model data definition found. Do you have a model'\
                   ' loaded?')
    return
  end

  data.each do |section, hValue|
    hValue.each do |key, value|
      yield section, key, value
    end
  end
end
meta_exist?(section, key) click to toggle source

return section/data existence

  • Args :

    • section : Section to search for data.

    • data : data name to check

  • Returns :

    • true/false

# File lib/lorj_meta.rb, line 451
def meta_exist?(section, key)
  p_exist?(:keys => [:sections, section, key])
end
section_data(section, key, *options) click to toggle source

Get model section/data options. It returns the list of options, against layers, of all Hash options, cloned and merged.

  • Args :

    • section : section name

    • data : data name

    • options : Optionnal. List of sub keys in tree to get data.

  • Returns :

    • Merged cloned data options values.

    OR

    • nil if:

      • missing section and data name as parameter.

      • data was not found. defined in /:sections/<section>/<data

      • data found is not an Array or a Hash.

# File lib/lorj_meta.rb, line 584
def section_data(section, key, *options)
  return nil if section.nil? || key.nil?
  keys = [:sections]
  keys << section << key
  keys.concat(options)
  p_get(:keys => keys, :merge => true)
end
sections(data = nil) click to toggle source

return the list of sections name of a data.

  • Args :

    • data : Optional. data name to search. If no name given, returns all existing sections.

  • Returns :

    • Array of sections name.

# File lib/lorj_meta.rb, line 511
def sections(data = nil)
  return p_get(:keys => [:sections]).keys if data.nil?

  _, data = _detect_section(data, nil)
  return nil unless p_exist?(:keys => [:keys, data])
  p_get(:keys => [:keys, data])
end
set(type, section, data_keys, options, layer = 'controller') click to toggle source

layer setting function

  • Args

    • type : Define the section type name. Predefined section type are :setup and :sections

    • section : Symbol. Section name of the data to define.

    • data : Symbol or Array of Symbols. Name of the data

    • options : Hash. List of options

    • layer : optional. Layer name to define. All layers are authorized, except 'app'/'keys'. 'app' is the protected application layer data. By default, the layer configured is 'controller'

  • Returns

    • The value set or nil

    OR

    • nil if type, section are not Symbol.

    • nil if data is not a Symbol or an Array of Symbol

    • nil if options is not a Hash

    • nil if layer is not a String or Nil.

    • nil if data is :keys or first element of the data is :keys. :keys is built internally to keep sections/keys mapping updated.

# File lib/lorj_meta.rb, line 641
def set(type, section, data_keys, options, layer = 'controller')
  return nil unless check_par(type,      Symbol,
                              section,   Symbol,
                              data_keys, [Symbol, Array],
                              options,   Hash,
                              layer,     [String, NilClass])

  keys = build_keys(type, section, data_keys)

  # :keys is a special sections used internally.
  return nil if keys[1] == :keys

  update_map(section, keys[2]) if keys[0] == :sections

  layer = 'controller' if layer.nil? || %w(app map).include?(layer)

  p_set(:keys => keys, :name => layer, :value => options)
end
setup_data(*options) click to toggle source

Get setup options. It returns the top layer data for options requested

  • Args :

    • options : Array of options tree.

  • Returns :

    • top layer data found.

    OR

    • nil if:

      • data was not found. defined in /:setup/options…

# File lib/lorj_meta.rb, line 562
def setup_data(*options)
  keys = [:setup]
  keys.concat(options)
  p_get(:keys => keys, :merge => true)
end
setup_options(*options) click to toggle source

Get model setup/data options. It returns the list of options, against layers, of all Hash options, cloned and merged. Warning! This function assumes data found to be a Hash or Array. To get the top layer data, use setup_data(options)

  • Args :

    • options : Array of setup options tree

  • Returns :

    • Merged cloned options values.

    OR

    • nil if:

      • missing section and data name as parameter.

      • data was not found. defined in /:sections/<section>/<data

      • data found is not an Array or a Hash.

# File lib/lorj_meta.rb, line 545
def setup_options(*options)
  keys = [:setup]
  keys.concat(options)
  p_get(:keys => keys, :merge => true)
end
update_controller_data(section, data, options, layer = 'controller') click to toggle source

Controller data definition which will enhance data Application definition. This function replace any controller definition. To replace or redefine options to an existing controller data, use define_data. You can also use [], []=, etc… provided by parent class. This will work only if the 'controller' is the highest layer.

  • Args:

    • section : Symbol. Section name of the data to define.

    • data : Symbol. Name of the data

    • options : Hash. List of options to add or update.

    • layer : optional. Layer name to define. All layers are authorized, except 'app'. 'app' is the protected application layer data. By default, the layer configured is 'controller'

# File lib/lorj_meta.rb, line 728
def update_controller_data(section, data, options, layer = 'controller')
  return nil unless check_par(section, Symbol,
                              data,    Symbol,
                              options, Hash,
                              layer,  [String, NilClass])

  keys = [:sections, section, data]
  value = p_get(:keys => keys, :name => 'controller')

  layer = 'controller' if layer.nil? || layer == 'app'

  p_set(:keys => keys, :name => layer, :value => value.merge(options))
end

Private Instance Methods

_detect_section(key, default_section) click to toggle source

Implement a section detection in a symbol/string Help each auto_* functions to work without update.

# File lib/lorj_meta.rb, line 64
def _detect_section(key, default_section)
  m = key.to_s.match(/^(.*)#(.*)$/)
  return [m[1].to_sym, m[2].to_sym] if m && m[1] != '' && m[2] != ''
  [default_section, key]
end
build_keys(type, section, data_keys) click to toggle source

build keys for set and del We assume type, section, data_keys to be with appropriate type.

# File lib/lorj_meta.rb, line 124
def build_keys(type, section, data_keys)
  keys = [type, section]

  return keys.concat(data_keys) if data_keys.is_a?(Array)
  keys << data_keys
end
build_section_mapping() click to toggle source

Used anytime config is updated (config data load or initialization)

This function rebuild the section/key mapping Usually, executed while initializing or while loading a config.

# File lib/lorj_meta.rb, line 40
def build_section_mapping
  return unless p_exist?(:keys => [:sections])

  # The primary data key should change from key to section & key.
  data = p_get(:keys => [:sections], :merge => true)

  return if data.nil?
  section_map = {}

  data.each do |section, values|
    next if values.nil?
    values.keys.each do |key|
      section_map[key] = [] unless section_map.key?(key)
      next if section_map[key].include?(section)

      section_map[key] << section
    end
  end
  p_set(:keys => [:keys], :value => section_map, :name => 'map')
end
check_par(*parameters) click to toggle source

parameters tester functions

  • Args Each data are defined by a couple parameters.

    • 1st one is the value to check

    • 2nd one is the expected class (Class object) or

acceptable classes (Array of class).

  • Returns

    • false if value is not of the expected Class.

    • true if all parameters are as expected.

# File lib/lorj_meta.rb, line 105
def check_par(*parameters)
  parameters.each_index do |index|
    next unless index.odd?

    if parameters[index].is_a?(Array)
      unless parameters[index].include?(parameters[index - 1].class)
        return false
      end
    end

    if parameters[index].is_a?(Class)
      return false unless parameters[index - 1].is_a?(parameters[index])
    end
  end
  true
end
define_meta_app_layer(data) click to toggle source

Define the meta_data Application layer It requires a Hash to initialize this layer until a file load is done instead. (Planned for Lorj 2.0)

# File lib/lorj_meta.rb, line 134
def define_meta_app_layer(data)
  data.delete(:keys) if data.is_a?(Hash) && data.key?(:keys)

  PRC::CoreConfig.define_layer(:name => 'app',
                               :config => PRC::BaseConfig.new(data),
                               :set => false)
end
define_meta_controller_layer() click to toggle source

Define the meta_data Application layer

# File lib/lorj_meta.rb, line 148
def define_meta_controller_layer
  PRC::CoreConfig.define_layer(:name => 'controller', :set => true,
                               :load => true)
end
define_meta_map_layer() click to toggle source

Define the meta_data section/keys mapping layer

# File lib/lorj_meta.rb, line 143
def define_meta_map_layer
  PRC::CoreConfig.define_layer(:name => 'map', :set => true)
end
delete_map(section, data) click to toggle source

delete section data mapping

# File lib/lorj_meta.rb, line 85
def delete_map(section, data)
  keys = [:keys, data]
  map = p_set(:keys => keys, :name => 'map')
  return if map.nil?

  map.remove section
end
update_map(section, data) click to toggle source

set section data mapping

# File lib/lorj_meta.rb, line 72
def update_map(section, data)
  keys = [:keys, data]
  map = p_get(:keys => keys, :name => 'map')
  if map.nil?
    map = [section]
  else
    map << section unless map.include?(section)
  end
  p_set(:keys => keys, :name => 'map', :value => map)
end