Class: PRC::BaseConfig
Overview
This class is Base config system of lorj.
It implements basic config features:
-
erase - To cleanup all data in self config
-
To get a value for a key or tree of keys
-
-
[]= - To set a value for a key in the tree.
-
exist? - To check the existence of a value from a key
-
del - To delete a key tree.
-
save - To save all data in a yaml file
-
load - To load data from a yaml file
-
data_options - To influence on how exist?, [], []=, load and save will
behave
Config Data are managed as Hash of Hashes. It uses actively Hash.rh_* functions. See rh.rb.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#filename ⇒ Object
Returns the value of attribute filename.
Instance Method Summary collapse
-
#[](*keys) ⇒ Object
Get function.
-
#[]=(*keys, value) ⇒ Object
Set function.
-
#data_options(options = nil) ⇒ Object
data_options set data options used by exist?, get, set, load and save functions.
-
#del(*keys) ⇒ Object
Set function.
-
#erase ⇒ Object
Erase function.
-
#exist?(*keys) ⇒ Boolean
exist?.
-
#initialize(value = nil) ⇒ BaseConfig
constructor
initialize BaseConfig.
-
#load(filename = nil) ⇒ Object
Load from a file.
-
#rh_key_to_symbol(level = 1) ⇒ Object
transform keys from string to symbol until deep level.
-
#rh_key_to_symbol?(level = 1) ⇒ Boolean
Check the need to transform keys from string to symbol until deep level.
-
#save(filename = nil) ⇒ Object
Save to a file.
- #to_s ⇒ Object
Constructor Details
#initialize(value = nil) ⇒ BaseConfig
initialize BaseConfig
-
Args
-
keys: Array of key path to found
-
-
Returns
-
boolean : true if the key path was found
-
ex: value = CoreConfig.New({ :test => => ‘found’}) # => creates a CoreConfig with this Hash of Hash
50 51 52 53 54 |
# File 'lib/prc_base_config.rb', line 50 def initialize(value = nil) @data = {} @data = value if value.is_a?(Hash) @data_options = {} # Options for exist?/set/get/load/save end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
36 37 38 |
# File 'lib/prc_base_config.rb', line 36 def data @data end |
#filename ⇒ Object
Returns the value of attribute filename.
37 38 39 |
# File 'lib/prc_base_config.rb', line 37 def filename @filename end |
Instance Method Details
#[](*keys) ⇒ Object
Get function
-
Args
-
keys: Array of key path to found
-
-
Returns -
132 133 134 |
# File 'lib/prc_base_config.rb', line 132 def [](*keys) _get(*keys) end |
#[]=(*keys, value) ⇒ Object
Set function
-
Args
-
keys: set a value in the Array of key path.
-
-
Returns
-
The value set or nil
-
ex: value = CoreConfig.New
value[:level1, :level2] = ‘value’ # => => {:level2 => ‘value’}
166 167 168 |
# File 'lib/prc_base_config.rb', line 166 def []=(*keys, value) _set(*keys, value) end |
#data_options(options = nil) ⇒ Object
data_options set data options used by exist?, get, set, load and save functions.
CoreConfig class type, call data_options to set options, before calling functions: exist?, get, set, load and save.
Currently, data_options implements:
-
:data_readonly : The data cannot be updated. set will not update
the value. -
:file_readonly : The file used to load data cannot be updated.
save will not update the file.
The child class can superseed or replace data options with their own options. Ex: If your child class want to introduce notion of sections, you can define the following with get: # by default, section name to use by get/set is :default def data_options(options = => :default)
()
end
def [](*keys)
_get(@data_options[:section], *keys)
end
def []=(*keys, value)
_set(@data_options[:section], *keys, value)
end
end
-
Args
-
keys: Array of key path to found
-
-
Returns
-
boolean : true if the key path was found
-
ex: { :test => => ‘found’}
95 96 97 |
# File 'lib/prc_base_config.rb', line 95 def ( = nil) end |
#del(*keys) ⇒ Object
Set function
-
Args
-
keys: set a value in the Array of key path.
-
-
Returns
-
The value set or nil
-
ex: value = CoreConfig.New
value[:level1, :level2] = ‘value’ # => => {:level2 => ‘value’}
150 151 152 |
# File 'lib/prc_base_config.rb', line 150 def del(*keys) _del(*keys) end |
#erase ⇒ Object
Erase function
-
Args
-
Returns -
120 121 122 |
# File 'lib/prc_base_config.rb', line 120 def erase @data = {} end |
#exist?(*keys) ⇒ Boolean
exist?
-
Args
-
keys: Array of key path to found
-
-
Returns
-
boolean : true if the key path was found
-
ex: { :test => => ‘found’}
109 110 111 |
# File 'lib/prc_base_config.rb', line 109 def exist?(*keys) _exist?(*keys) end |
#load(filename = nil) ⇒ Object
Load from a file
-
Args :
-
filename: file name to load. This file name will become the defaultfile name to use next time.
-
-
Returns :
-
true if loaded.
-
-
Raises :
-
++ ->
-
179 180 181 |
# File 'lib/prc_base_config.rb', line 179 def load(filename = nil) _load(filename) end |
#rh_key_to_symbol(level = 1) ⇒ Object
transform keys from string to symbol until deep level. Default is 1.
-
Args :
-
level: Default 1. level to transform
-
-
Returns :
-
it self, with config updated.
-
201 202 203 |
# File 'lib/prc_base_config.rb', line 201 def rh_key_to_symbol(level = 1) data.rh_key_to_symbol level end |
#rh_key_to_symbol?(level = 1) ⇒ Boolean
Check the need to transform keys from string to symbol until deep level. Default is 1.
-
Args :
-
level: Default 1: levels to verify
-
-
Returns :
-
true if need to be updated.
-
214 215 216 |
# File 'lib/prc_base_config.rb', line 214 def rh_key_to_symbol?(level = 1) data.rh_key_to_symbol? level end |
#save(filename = nil) ⇒ Object
Save to a file
-
Args :
-
filename: file name to save. This file name will become the defaultfile name to use next time.
-
-
Returns :
-
boolean if saved or not. true = saved.
-
190 191 192 |
# File 'lib/prc_base_config.rb', line 190 def save(filename = nil) _save(filename) end |
#to_s ⇒ Object
228 229 230 231 232 |
# File 'lib/prc_base_config.rb', line 228 def to_s msg = format("File : %s\n", @filename) msg += data.to_yaml msg end |