Class: PRC::BaseConfig

Inherits:
Object show all
Defined in:
lib/prc_base_config.rb

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

SectionConfig

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject (readonly)

Returns the value of attribute data.



36
37
38
# File 'lib/prc_base_config.rb', line 36

def data
  @data
end

#filenameObject

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)

_data_options(options)

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 data_options(options = nil)
  _data_options options
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

#eraseObject

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’}

Returns:

  • (Boolean)


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 default

      file 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.

Returns:

  • (Boolean)


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 default

      file 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_sObject



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