Class: PRC::BaseConfig

Inherits:
Object show all
Includes:
PRC::BaseConfigRubySpec::Private, PRC::BaseConfigRubySpec::Public
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

Methods included from PRC::BaseConfigRubySpec::Private

#p_set

Methods included from PRC::BaseConfigRubySpec::Public

#[]=

Constructor Details

#initialize(value = nil, latest_version = 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



70
71
72
73
74
75
76
# File 'lib/prc_base_config.rb', line 70

def initialize(value = nil, latest_version = nil)
  @data = {}
  @data = value if value.is_a?(Hash)
  @data_options = {} # Options for exist?/set/get/load/save
  @latest_version = latest_version
  @version = latest_version
end

Instance Attribute Details

#dataObject (readonly)

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



46
47
48
# File 'lib/prc_base_config.rb', line 46

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.



51
52
53
# File 'lib/prc_base_config.rb', line 51

def filename
  @filename
end

#latest_versionObject (readonly)

config layer latest version



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

def latest_version
  @latest_version
end

#versionObject

config layer version



54
55
56
# File 'lib/prc_base_config.rb', line 54

def version
  @version
end

Instance Method Details

#[](*keys) ⇒ Object

Get function

  • Args

    • keys : Array of key path to found

  • Returns -



151
152
153
# File 'lib/prc_base_config.rb', line 151

def [](*keys)
  p_get(*keys)
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



115
116
117
# File 'lib/prc_base_config.rb', line 115

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



169
170
171
# File 'lib/prc_base_config.rb', line 169

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

#eraseObject

Erase the data in the object. internal version is cleared as well.

  • Returns

    • Hash : {}.



138
139
140
141
# File 'lib/prc_base_config.rb', line 138

def erase
  @version = @latest_version
  @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)


129
130
131
# File 'lib/prc_base_config.rb', line 129

def exist?(*keys)
  p_exist?(*keys)
end

#latest_version?Boolean

Returns:

  • (Boolean)


256
257
258
# File 'lib/prc_base_config.rb', line 256

def latest_version?
  (@version == @latest_version)
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 :

    • ++ ->



182
183
184
# File 'lib/prc_base_config.rb', line 182

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.



222
223
224
# File 'lib/prc_base_config.rb', line 222

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)


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

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.



193
194
195
# File 'lib/prc_base_config.rb', line 193

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

#to_sObject

Print a representation of the Layer data



250
251
252
253
254
# File 'lib/prc_base_config.rb', line 250

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

#where?(_keys, name) ⇒ Boolean

where layer helper format Used by CoreConfig where?

In the context of CoreConfig, this class is a layer with a name. CoreConfig will query this function to get a layer name. If the layer needs to add any other data, this function will need to be redefined.

  • Args :

    • name : name of this layer managed by CoreConfig

  • Returns :

    • name: Composed layer name return by the layer to CoreConfig It returns simply name.

Returns:

  • (Boolean)


211
212
213
# File 'lib/prc_base_config.rb', line 211

def where?(_keys, name)
  name
end