Class: Lorj::MetaAppConfig
- Inherits:
-
PRC::CoreConfig
- Object
- PRC::CoreConfig
- Lorj::MetaAppConfig
- Defined in:
- lib/lorj_meta.rb,
lib/lorj_meta.rb
Overview
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:
-
:setup: Contains :ask_step array
-
:ask_step:
Array of group of keys/values to setup. Each group will be internally identified by a index starting at 0. parameters are as follow:
-
:desc: string to print out before group setup
ERB template enable: To get config data in ERB context, use config
-
:explanation: longer string to display after :desc:
It is printed out in brown color.
It supports ERB template. To get config data, type <%= config %>
In your defaults.yaml file, write multiline with |- after the key.
Ex: if config returns ‘text’, defaults.yaml can have the following explanation.
:setup: :ask_step: - :desc: 'Small description' :explanation: |- My complete explanation is in multiline <%= config['text'] %>By default, thanks to data model dependency, the group is automatically populated. So, you need update this part only for data that are not found from the dependency.
-
:add: array of keys to add manually in the group. The Array can be written with [] or list of dash elements
Example of a defaults.yaml content:
:setup: :ask_step: - :desc: 'Small description' :add: [:key_pair_files, :ssh_user]
-
-
-
:section: Contains a list of sections with several keys and attributes and eventually :default:
This list of sections and keys will be used to build the account files with the lorj Lorj::Core.setup function. Those data is accessible through the Lorj.defaults.get_meta, Lorj.defaults.get_meta_auto or Lorj.defaults.get_meta_section
please note that Lorj.defaults.get_meta uses the controller config layer to redefine defaults application meta data on controller needs. See BaseDefinition.define_data for details.
Ex:
# Use Lorj.defaults.data exceptionnaly Lorj.defaults.data.merge({sections: {:mysection: {key: { data1: 'test1', data2: 'test2' } } } }) puts Lorj.defaults.get_meta(:mysection, :key) # => { data1: 'test1', data2: 'test2' } puts Lorj.defaults.get_meta(:mysection) # => {:key => { data1: 'test1', data2: 'test2' }} puts Lorj.defaults.get_meta_section(:key) # => :mysection puts Lorj.defaults.get_meta_auto(:key) # => { data1: 'test1', data2: 'test2' }-
:default: This section define updatable data available from config.yaml. But will never be added in an account file.
It contains a list of key and options.
-
:<aKey>: Possible options
-
:desc: default description for that <aKey>
-
-
-
:<aSectionName>: Name of the section which should contains a list
-
:<aKeyName>: Name of the key to setup.
-
:desc:
Description of that key, printed out at setup time. default: nil
-
:explanation: Multi line explanation. In yaml, use |- to write multilines
Print a multiline explanation before ask the key value. ERB template enable. To get config data, type <%= config %>
-
:encrypted: true if this has to be encrypted with ~/.cache/forj/.key
-
:readonly: true if this key is not modifiable by a simple. Lorj::Account::set function. false otherwise. Default: false
-
:account_exclusive: true to limit the data to account config layer.
-
:account: Set to true for setup to ask this data to the user during setup process. default: false
-
:validate: Ruby Regex to validate the end user input.
Ex: :validate: !ruby/regexp /^w?w*$/
-
:default: Default value. Replace /:default/<data>
-
:default_value: default value proposed to the user.
-
:ask_step: Define the group number to attach the key to be asked.
By default, setup will determine the step, thanks to lorj object dependencies tree.
This number start at 0. Each step can be defined by /:setup/:ask_step/<steps> list. See :setup section.
ex:
:sections: :mysection: :mydata: :setup: :ask_step: 2 -
:ask_sort: Number which represents the ask order in the step group. (See /:setup/:ask_step for details)
-
:after: <Data> Name of the previous <Data> to ask before the current one.
-
:depends_on: Identify :data type required to be set before the current one.
-
:value_mapping: list of values to map as defined by the controller
-
:controller: mapping for get controller value from process values
<value> : <map> value map equivalence. See data_value_mapping function
-
-
:process: mapping for get process value from controller values
<value> : <map> value map equivalence. See data_value_mapping function
-
:list_values: Provide capabililities to get a list and choose from.
-
:query_type: It can be:
-
:query_call to execute a query on flavor, query_params is empty for all. Data are extracted thanks to :values.
-
:process_call to execute a process function to get the values. Data are extracted thanks to :values.
-
:controller_call to execute a controller query. Data are extracted thanks to :values.
-
:values to get list of fixed values from :values.
-
-
:object:
Used with :query_type=:query_call. object type symbol to query.
-
:query
Used with :query_type=:process_call. process function name to call
-
:query_call:
Used with :query_type=:controller_call. Handler function to use. (query_e, create_e, …) The function called must return an Array.
Used with :query_type=:process_call. Function name to call
-
:query_params:
Used with :query_type=:query_call. Query hash defining filtering capabilities.
Used with :query_type=:process_call. hParams data passed to the process function.
-
:values: List of fields to get values or list of fixed values.
Depends on query type.
-
:validate:
if :list_strict, the value is limited to the possible values from the list
-
-
:pre_step_function: Process called before asking the data.
if it returns true, user interaction is cancelled.
-
:post_step_function:Process called after asking the data.
if it returns false, the user is requested to re-enter a new value.
-
-
-
Instance Method Summary collapse
-
#[]=(type, section, *keys, options) ⇒ Object
layer setting function.
-
#auto_meta_exist?(data) ⇒ Boolean
return 1st section/data existence.
-
#auto_section_data(data, *options) ⇒ Object
Get model data options.
-
#datas ⇒ Object
return the list of valid keys found in meta data.
-
#define_controller_data(section, data, options, layer = 'controller') ⇒ Object
Controller data definition which will enhance data Application definition.
-
#del(type, section, data_keys, layer = 'controller') ⇒ Object
section/data removal function.
-
#first_section(data) ⇒ Object
return the 1st section name of a data.
-
#initialize(data) ⇒ MetaAppConfig
constructor
Implements a 2 layers metadata config.
-
#meta_each ⇒ Object
Loop on Config metadata.
-
#meta_exist?(section, key) ⇒ Boolean
return section/data existence.
-
#section_data(section, key, *options) ⇒ Object
Get model section/data options.
-
#sections(data = nil) ⇒ Object
return the list of sections name of a data.
-
#set(type, section, data_keys, options, layer = 'controller') ⇒ Object
layer setting function.
-
#setup_data(*options) ⇒ Object
Get setup options.
-
#setup_options(*options) ⇒ Object
Get model setup/data options.
-
#update_controller_data(section, data, options, layer = 'controller') ⇒ Object
Controller data definition which will enhance data Application definition.
Methods inherited from PRC::CoreConfig
#[], define_layer, #exist?, #file, #layer_add, #layer_index, #layer_indexes, #layer_remove, #layers, #load, #merge, #mergeable?, #save, #to_s, #where?
Constructor Details
#initialize(data) ⇒ MetaAppConfig
Implements a 2 layers metadata config.
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/lorj_meta.rb', line 366 def initialize(data) config_layers = [] # Application layer config_layers << (data) # mapping section/keys layer config_layers << # controller Config layer config_layers << initialize_layers(config_layers) build_section_mapping end |
Instance Method Details
#[]=(type, section, *keys, options) ⇒ Object
layer setting function
-
Args
-
type: :sections by default. Define the section type name. -
section: Symbol. Section name of the data to define. -
keys: 1 Symbol or more Symbols. Name of the data and options. -
options: Hash. List of options
-
-
Returns
-
The value set or nil
-
613 614 615 616 |
# File 'lib/lorj_meta.rb', line 613 def []=(type, section, *keys, ) return nil if keys.length == 0 set(type, section, keys, ) end |
#auto_meta_exist?(data) ⇒ Boolean
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.
The list of sections for one key is build thanks to build_section_mapping.
-
Args :
-
data: data name to check
-
-
Returns :
-
true/false
-
427 428 429 430 431 432 |
# File 'lib/lorj_meta.rb', line 427 def (data) return nil unless data section = first_section(data) p_exist?(:keys => [:sections, section, data]) end |
#auto_section_data(data, *options) ⇒ Object
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.
The list of sections for one key is build thanks to build_section_mapping.
-
Args :
-
data: data name -
options+ : options tree.
-
-
Returns :
-
data options values
OR
-
nil if:
-
missing data name as parameter.
-
data was not found. defined in /:sections/<section>/<data
-
-
555 556 557 558 559 |
# File 'lib/lorj_meta.rb', line 555 def auto_section_data(data, *) return nil if data.nil? section = first_section(data) section_data(section, data, *) end |
#datas ⇒ Object
return the list of valid keys found in meta data.
-
Args :
-
Returns :
-
Array of data name.
-
469 470 471 |
# File 'lib/lorj_meta.rb', line 469 def datas p_get(:keys => [:keys], :name => 'map').keys end |
#define_controller_data(section, data, options, layer = 'controller') ⇒ Object
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’
-
657 658 659 660 661 662 663 664 665 666 667 668 669 670 |
# File 'lib/lorj_meta.rb', line 657 def define_controller_data(section, data, , layer = 'controller') return nil unless check_par(section, Symbol, data, Symbol, , 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 => ) end |
#del(type, section, data_keys, layer = 'controller') ⇒ Object
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
-
629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
# File 'lib/lorj_meta.rb', line 629 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) ⇒ Object
return the 1st section name of a data.
-
Args :
-
data: data name to search
-
-
Returns :
-
1st section name found.
-
441 442 443 444 445 446 |
# File 'lib/lorj_meta.rb', line 441 def first_section(data) return nil unless p_exist?(:keys => [:keys, data]) arr = p_get(:keys => [:keys, data]) return nil unless arr.is_a?(Array) && arr[0] arr[0] end |
#meta_each ⇒ Object
Loop on Config metadata
-
Args :
-
code: Block of code onsection,key,value
-
-
Returns :
-
nothing
-
390 391 392 393 394 395 396 397 398 |
# File 'lib/lorj_meta.rb', line 390 def data = p_get(:keys => [:sections], :merge => true) data.each do |section, hValue| hValue.each do |key, value| yield section, key, value end end end |
#meta_exist?(section, key) ⇒ Boolean
return section/data existence
-
Args :
-
section: Section to search for data. -
data: data name to check
-
-
Returns :
-
true/false
-
408 409 410 |
# File 'lib/lorj_meta.rb', line 408 def (section, key) p_exist?(:keys => [:sections, section, key]) end |
#section_data(section, key, *options) ⇒ Object
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: options tree.
-
-
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.
-
-
528 529 530 531 532 533 534 |
# File 'lib/lorj_meta.rb', line 528 def section_data(section, key, *) return nil if section.nil? || key.nil? keys = [:sections] keys << section << key keys.concat() p_get(:keys => keys, :merge => true) end |
#sections(data = nil) ⇒ Object
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.
-
456 457 458 459 460 461 |
# File 'lib/lorj_meta.rb', line 456 def sections(data = nil) return p_get(:keys => [:sections]).keys if data.nil? return nil unless p_exist?(:keys => [:keys, data]) p_get(:keys => [:keys, data]) end |
#set(type, section, data_keys, options, layer = 'controller') ⇒ Object
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.
-
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
# File 'lib/lorj_meta.rb', line 583 def set(type, section, data_keys, , layer = 'controller') return nil unless check_par(type, Symbol, section, Symbol, data_keys, [Symbol, Array], , 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 => ) end |
#setup_data(*options) ⇒ Object
Get setup options. It returns the 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…
-
-
506 507 508 509 510 |
# File 'lib/lorj_meta.rb', line 506 def setup_data(*) keys = [:setup] keys.concat() p_get(:keys => keys) end |
#setup_options(*options) ⇒ Object
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.
-
-
489 490 491 492 493 |
# File 'lib/lorj_meta.rb', line 489 def (*) keys = [:setup] keys.concat() p_get(:keys => keys, :merge => true) end |
#update_controller_data(section, data, options, layer = 'controller') ⇒ Object
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’
-
686 687 688 689 690 691 692 693 694 695 696 697 698 |
# File 'lib/lorj_meta.rb', line 686 def update_controller_data(section, data, , layer = 'controller') return nil unless check_par(section, Symbol, data, Symbol, , 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()) end |