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



56
57
58
59
60
# File 'lib/prc_base_config.rb', line 56

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)

internal Hash data of this config. Do not use it except if you know what you are doing.



38
39
40
# File 'lib/prc_base_config.rb', line 38

def data
  @data
end

#filenameObject

  • set: set the config file name. It accepts relative or absolute path to the file.

  • get: get the config file name used by #load and #save.



43
44
45
# File 'lib/prc_base_config.rb', line 43

def filename
  @filename
end

Instance Method Details

#[](*keys) ⇒ Object

Get function

  • Args

    • keys : Array of key path to found

  • Returns -



136
137
138
# File 'lib/prc_base_config.rb', line 136

def [](*keys)
  p_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'
# => {:level1 => {:level2 => 'value'}}


171
172
173
# File 'lib/prc_base_config.rb', line 171

def []=(*keys, value)
  p_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:

class MySection < PRC::BaseConfig
  # by default, section name to use by get/set is :default
  def data_options(options = {:section => :default})
    p_data_options(options)
  end

  def [](*keys)
    p_get(@data_options[:section], *keys)
  end

  def []=(*keys, value)
    p_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



99
100
101
# File 'lib/prc_base_config.rb', line 99

def data_options(options = nil)
  p_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’}



154
155
156
# File 'lib/prc_base_config.rb', line 154

def del(*keys)
  p_del(*keys)
end

#eraseObject

Erase function

  • Args

  • Returns -



124
125
126
# File 'lib/prc_base_config.rb', line 124

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)


113
114
115
# File 'lib/prc_base_config.rb', line 113

def exist?(*keys)
  p_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 :

    • ++ ->



184
185
186
# File 'lib/prc_base_config.rb', line 184

def load(filename = nil)
  p_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.



206
207
208
# File 'lib/prc_base_config.rb', line 206

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)


219
220
221
# File 'lib/prc_base_config.rb', line 219

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.



195
196
197
# File 'lib/prc_base_config.rb', line 195

def save(filename = nil)
  p_save(filename)
end

#to_sObject

Print a representation of the Layer data



234
235
236
237
238
# File 'lib/prc_base_config.rb', line 234

def to_s
  msg = format("File : %s\n", @filename)
  msg += data.to_yaml
  msg
end