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
Instance Method Summary collapse
-
#[](*keys) ⇒ Object
Get function.
-
#[]=(*keys, value) ⇒ Object
Set function.
- #_build_layers(result) ⇒ Object
-
#_common_options_get(options, required = [], optionnal = []) ⇒ Object
Take care of keys, indexes and names.
- #_keys_data_missing(options, required, result) ⇒ Object
-
#_layer_indexes(names) ⇒ Object
layer_indexes function.
- #_set_data_opts(result) ⇒ Object
-
#_set_indexes(result) ⇒ Object
Setting indexes from names or indexes.
-
#_valid_options(options, required, optionnal = []) ⇒ Object
Check and returns values of options required and optionnal.
-
#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.
- #initialize_layer(layer) ⇒ Object
-
#initialize_layers(config_layers = nil) ⇒ Object
Function to initialize Config layers.
-
#layer_index(name) ⇒ Object
layer_index function.
- #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:
- _set_data_options(layers, options) - To set data_options on one or
more config layers
- _get(options) - core get function
- _set(options) - core set function
- _save(options) - core save function
- _load(options) - 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`
-
645 646 647 648 649 650 651 |
# File 'lib/prc_core_config.rb', line 645 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
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
# File 'lib/prc_core_config.rb', line 653 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 |
#_build_layers(result) ⇒ Object
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 |
# File 'lib/prc_core_config.rb', line 860 def _build_layers(result) # Setting layers at required [0] if result[1][0].nil? || result[1][0].length == 0 config_layers = @config_layers else config_layers = [] result[1][0].each do | index | config_layers << @config_layers[index] if index.is_a?(Fixnum) end config_layers = @config_layers if config_layers.length == 0 end result[0].insert(0, config_layers) # And removing the optionnal indexes result[1].delete_at(0) result end |
#_common_options_get(options, required = [], optionnal = []) ⇒ Object
Take care of keys, indexes and names. if names if found, it will replace indexes.
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 |
# File 'lib/prc_core_config.rb', line 796 def (, required = [], optionnal = []) return nil unless .is_a?(Hash) required = [] unless required.is_a?(Array) optionnal = [] unless optionnal.is_a?(Array) result = (, required, [:indexes, :names, :data_options].concat(optionnal)) # result Array is structured as: # required [0] => [...] # optional [1] => [indexes, names, data_options, ...] # Following eliminates the optional :names (1) element _set_indexes result # required [0] => [...] # optional [1] => [indexes, data_options, ...] return nil if _keys_data_missing(, required, result) # Following eliminates the optional :indexes (0) element # But add it as required in the Array, at pos 0 _build_layers(result) # required [0] => [layers, ...] # optional [1] => [data_options, ...] # following eliminates the optional :data_options (0) element # But data_options is added in required Array, at pos 1. _set_data_opts(result) # required [0] => [layers, data_options, ...] # optional [1] => [...] result end |
#_keys_data_missing(options, required, result) ⇒ Object
829 830 831 832 833 834 835 836 |
# File 'lib/prc_core_config.rb', line 829 def _keys_data_missing(, required, result) return false unless required[0] == :keys return true unless .key?(:keys) return true unless result[0][0].is_a?(Array) return true if result[0][0].length == 0 false end |
#_layer_indexes(names) ⇒ Object
layer_indexes function
-
Args
-
:name: layer to identify. -
Returns first index found or nil.
734 735 736 737 738 739 740 741 742 743 744 745 746 |
# File 'lib/prc_core_config.rb', line 734 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 |
#_set_data_opts(result) ⇒ Object
846 847 848 849 850 851 852 853 854 855 856 857 858 |
# File 'lib/prc_core_config.rb', line 846 def _set_data_opts(result) data_opts = [] result[0][0].each_index do | layer_index | = result[1][0][layer_index] if result[1][0].is_a?(Array) = {} unless .is_a?(Hash) data_opts << end result[0].insert(1, data_opts) # And removing the optionnal :data_options result[1].delete_at(0) end |
#_set_indexes(result) ⇒ Object
Setting indexes from names or indexes.
839 840 841 842 843 844 |
# File 'lib/prc_core_config.rb', line 839 def _set_indexes(result) names_indexes = _layer_indexes(result[1][1]) # replaced indexes by names indexes if exists. result[1][0] = names_indexes if names_indexes result[1].delete_at(1) end |
#_valid_options(options, required, optionnal = []) ⇒ Object
Check and returns values of options required and optionnal.
-
Args
-
options: options to extract information. -
required: Array of required option keys. -
optionnal: Array of optionnal option keys. -
Returns
-
nil if at least one required keys doesn’t exist
-
Array of combined required and optionnql values.
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 |
# File 'lib/prc_core_config.rb', line 777 def (, required, optionnal = []) return nil unless .is_a?(Hash) return nil unless required.is_a?(Array) optionnal = [] unless optionnal.is_a?(Array) result = [[], []] required.each do | key | return nil unless .key?(key) result[0] << [key] end optionnal.each { | key | result[1] << [key] } result 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 |
#initialize_layer(layer) ⇒ Object
717 718 719 720 721 722 723 724 |
# File 'lib/prc_core_config.rb', line 717 def initialize_layer(layer) newlayer = { :config => layer[:config], :name => layer[:name] } newlayer[:set] = layer[:set].boolean? ? layer[:set] : true newlayer[:load] = layer[:load].boolean? ? layer[:load] : false newlayer[:save] = layer[:save].boolean? ? layer[:save] : false newlayer[:file_set] = layer[:file_set].boolean? ? layer[:file_set] : false newlayer end |
#initialize_layers(config_layers = nil) ⇒ Object
Function to initialize Config layers.
Args
-
config_layers: Array of config layers.First layer is the deepest config object. last layer is the first config object queried.
Ex: If we define 2 config layer: local = PRC::BaseConfig.new(:test => :found_local) runtime = PRC::BaseConfig.new(:test => :found_runtime) layers = [] layers << CoreConfig.define_layer(name: ‘local’, config: local ) layers << CoreConfig.define_layer(name: ‘runtime’, config: runtime ) config = PRC::CoreConfig.new config.initialize_layers(layers)
p config # => :found_runtime
config = “where?” p config.where?(:test) # => [“runtime”, “local”]
config.del(:test) p config.where?(:test) # => [“local”] p config # => :found_local
config = “and now?” p config.where?(:test) # => [“runtime”, “local”]
705 706 707 708 709 710 711 712 713 714 715 |
# File 'lib/prc_core_config.rb', line 705 def initialize_layers(config_layers = nil) @config_layers = [] config_layers.each do | layer | next unless layer.is_a?(Hash) && layer.key?(:config) && layer[:config].is_a?(BaseConfig) next unless layer[:name].is_a?(String) @config_layers << initialize_layer(layer) end @config_layers.reverse! end |
#layer_index(name) ⇒ Object
layer_index function
-
Args
-
:name: layer to identify. -
Returns first index found or nil.
756 757 758 759 760 761 762 763 764 |
# File 'lib/prc_core_config.rb', line 756 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 |
#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 |
# 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]) 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 |