Class: PRC::CoreConfig
- Defined in:
- lib/prc_core_config.rb,
lib/prc_core_config.rb,
lib/prc_core_config.rb
Overview
Internal functions
Direct Known Subclasses
Class Method Summary collapse
-
.define_layer(options = {}) ⇒ Object
Function to define layer options.
Instance Method Summary collapse
-
#[](*keys) ⇒ Object
Get function.
-
#[]=(*keys, value) ⇒ Object
Set function.
-
#del(*keys) ⇒ Object
Del function.
-
#exist?(*keys) ⇒ Boolean
exist?.
-
#file(filename = nil, options = {}) ⇒ Object
Get/Set the file name.
-
#initialize(config_layers = nil) ⇒ CoreConfig
constructor
initialize CoreConfig.
-
#layer_add(options) ⇒ Object
This function add a config layer at runtime.
-
#layer_index(name) ⇒ Object
layer_index function.
-
#layer_indexes(names) ⇒ Object
layer_indexes function.
-
#layer_remove(options) ⇒ Object
Function to remove a runtime layer.
- #layers ⇒ Object
-
#load(options = {}) ⇒ Object
Load from a file to the highest layer or a specific layer.
-
#save(options = {}) ⇒ Object
Save to a file.
- #to_s ⇒ Object
-
#where?(*keys) ⇒ Boolean
where?.
Constructor Details
#initialize(config_layers = nil) ⇒ CoreConfig
initialize CoreConfig
-
Args
-
config_layers: Array config layers configuration. Each layer options have those options:-
:config : optional. See ‘Defining Config layer instance` for details
-
:name : required. String. Name of the config layer.
Warning! unique name on layers is no tested. -
:set : boolean. True if authorized. Default is True.
-
:load : boolean. True if authorized. Default is False.
-
:save : boolean. True if authorized. Default is False.
-
:file_set : boolean. True if authorized to update a filename.
Default is False.
-
each layers can defines some options for the layer to behave differently CoreConfig call a layer data_options to set some options, before exist?, get or [], set or []=, save and load functions. See BaseConfig::data_options for predefined options.
Core config provides some private additionnal functions for child class functions:
- (layers, ) - To set on one or
more config layers
- _get() - core get function
- _set() - core set function
- _save() - core save function
- _load() - core load function
if config_layers is not provided, CoreConfig will instanciate a runtime like system: config = CoreConfig.New # is equivalent as : config_layers = [‘runtime’,
config: PRC::BaseConfig.new, set: true]
config = CoreConfig.New(config_layers)
Defining Config layer instance:
:config value requires it to be of type ‘BaseConfig’ By default, it uses ‘:config => PRC::BaseConfig.new` Instead, you can set:
-
directly BaseConfig. ‘:config => PRC::BaseConfig.new`
-
a child based on BaseConfig. ‘:config => MyConfig.new`
-
some predefined enhanced BaseConfig:
-
PRC::SectionConfig. See prc_section_config.rb. ‘:config => PRC::SectionConfig.new`
-
646 647 648 649 650 651 652 |
# File 'lib/prc_core_config.rb', line 646 def initialize(config_layers = nil) if config_layers.nil? config_layers = [] config_layers << CoreConfig.define_layer end initialize_layers(config_layers) end |
Class Method Details
.define_layer(options = {}) ⇒ Object
Function to define layer options. By default, :set is true and :config is attached to a new PRC::BaseConfig instance.
Supported options:
- :config : optional. See `Defining Config layer instance` for details
- :name : required. String. Name of the config layer.
Warning! unique name on layers is no tested.
- :set : boolean. True if . Default is True.
- :load : boolean. True if . Default is False.
- :save : boolean. True if . Default is False.
- :file_set : boolean. True if to update a filename.
Default is False.
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 |
# File 'lib/prc_core_config.rb', line 730 def self.define_layer( = {}) attributes = [:name, :config, :set, :load, :save, :file_set] layer = {} attributes.each do |attribute| if .key?(attribute) layer[attribute] = [attribute] else layer[attribute] = case attribute when :name 'runtime' when :config PRC::BaseConfig.new when :set true else false end end end layer end |
Instance Method Details
#[](*keys) ⇒ Object
Get function
-
Args
-
keys: Array of key path to found
-
-
Returns value found or nil.
128 129 130 |
# File 'lib/prc_core_config.rb', line 128 def [](*keys) _get(:keys => keys) end |
#[]=(*keys, value) ⇒ Object
Set function
-
Args
-
keys: Array of key path to found
-
-
Returns
-
The value set or nil
-
ex: value = CoreConfig.New
value[:level1, :level2] = ‘value’ # => => {:level2 => ‘value’}
144 145 146 |
# File 'lib/prc_core_config.rb', line 144 def []=(*keys, value) _set(:keys => keys, :value => value) end |
#del(*keys) ⇒ Object
Del function
-
Args
-
keys: Array of key path to found and delete the last element.
-
-
Returns
-
The Hash updated.
-
ex: value = CoreConfig.New
value[:level1, :level2] = ‘value’ # => => {:level2 => ‘value’} => {:level2 => ‘value’}.del(:level1, :level2) # => => {}
162 163 164 |
# File 'lib/prc_core_config.rb', line 162 def del(*keys) _del(:keys => keys) end |
#exist?(*keys) ⇒ Boolean
exist?
-
Args
-
keys: Array of key path to found
-
-
Returns
-
boolean : true if the key path was found
-
Class child: A class child can redefine this function to increase default features.
104 105 106 |
# File 'lib/prc_core_config.rb', line 104 def exist?(*keys) _exist?(:keys => keys) end |
#file(filename = nil, options = {}) ⇒ Object
Get/Set the file name.
-
Args
-
:file: file name for the layer identified. -
options: Supported options for save-
:index: layer index to get data. -
:name: layer to get data.If neither :name or :index is set, nil is returned.
-
-
Returns
-
The file name.
-
209 210 211 |
# File 'lib/prc_core_config.rb', line 209 def file(filename = nil, = {}) _file(filename, ) end |
#layer_add(options) ⇒ Object
This function add a config layer at runtime. The new layer added at runtime, can be removed at runtime with layer_remove The name MUST be different than other existing config layer names
Args
-
options: Hash data-
:name : Required. Name of the layer to add
-
:index : Config position to use. 0 is the default. 0 is the first
Config layer use by get. -
:config : A Config instance of class type PRC::BaseConfig
-
:set : Boolean. True if is authorized to set a variable.
-
:load : Boolean. True if is authorized to load from a file.
-
:save : Boolean. True if is authorized to save to a file.
-
:file_set : Boolean. True if is authorized to change the file name.
-
returns
-
true if layer is added.
OR
-
nil : if layer name already exist
674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
# File 'lib/prc_core_config.rb', line 674 def layer_add() layer = CoreConfig.define_layer() layer[:init] = false # Runtime layer index = 0 index = [:index] if [:index].is_a?(Fixnum) names = [] @config_layers.each { |alayer| names << alayer[:name] } return nil if names.include?(layer[:name]) @config_layers.insert(index, layer) true end |
#layer_index(name) ⇒ Object
layer_index function
-
Args
-
:name: layer to identify. -
Returns first index found or nil.
784 785 786 787 788 789 790 791 792 |
# File 'lib/prc_core_config.rb', line 784 def layer_index(name) return nil unless name.is_a?(String) return nil if @config_layers.nil? @config_layers.each_index do |index| return index if @config_layers[index][:name] == name end nil end |
#layer_indexes(names) ⇒ Object
layer_indexes function
-
Args
-
:name: layer to identify. -
Returns first index found or nil.
762 763 764 765 766 767 768 769 770 771 772 773 774 |
# File 'lib/prc_core_config.rb', line 762 def layer_indexes(names) names = [names] if names.is_a?(String) return nil unless names.is_a?(Array) layers = [] names.each do |name| index = layer_index(name) layers << index unless index.nil? end return layers if layers.length > 0 nil end |
#layer_remove(options) ⇒ Object
Function to remove a runtime layer. You cannot remove a predefined layer, created during CoreConfig instanciation. Args
-
options: Hash data-
:name: Name of the layer to remove. -
:index: Index of the layer to remove.
-
At least, :name or :index is required. If both; :name and :index are set, :name is used. return
-
true if layer name is removed.
OR
-
nil : if not found or invalid.
703 704 705 706 707 708 709 710 711 712 713 714 715 |
# File 'lib/prc_core_config.rb', line 703 def layer_remove() index = layer_index([:name]) index = [:index] if index.nil? return nil if index.nil? layer = @config_layers[index] return nil if layer.nil? || layer[:init] @config_layers.delete_at(index) true end |
#layers ⇒ Object
213 214 215 216 217 |
# File 'lib/prc_core_config.rb', line 213 def layers result = [] @config_layers.each { |layer| result << layer[:name] } result end |
#load(options = {}) ⇒ Object
Load from a file to the highest layer or a specific layer.
-
Args :
-
options: Supported options for load-
:name: layer to get data. -
:index: layer index to get data.If neither :name or :index is set, set will use the highest layer
-
-
Returns : -
-
Raises :
-
++ ->
-
179 180 181 |
# File 'lib/prc_core_config.rb', line 179 def load( = {}) _load() end |
#save(options = {}) ⇒ Object
Save to a file
-
Args :
-
options: Supported options for save-
:name: layer to get data. -
:index: layer index to get data.If neither :name or :index is set, set will use the highest layer
-
-
Returns : -
194 195 196 |
# File 'lib/prc_core_config.rb', line 194 def save( = {}) _save() end |
#to_s ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/prc_core_config.rb', line 219 def to_s data = "Configs list ordered:\n" @config_layers.each do |layer| data += format("---- Config : %s ----\noptions: ", layer[:name]) data += 'predefined, ' if layer[:init].is_a?(TrueClass) if layer[:set] data += 'data RW ' else data += 'data RO ' end data += format(", %s\n", to_s_file_opts(layer)) data += format("%s\n", layer[:config].to_s) end data end |
#where?(*keys) ⇒ Boolean
where?
-
Args
-
keys: Array of key path to found
-
-
Returns
-
boolean : true if the key path was found
-
116 117 118 |
# File 'lib/prc_core_config.rb', line 116 def where?(*keys) _where?(:keys => keys) end |