Class: Lorj::Config

Inherits:
PRC::CoreConfig
  • Object
show all
Defined in:
lib/lorj_config.rb,
lib/lorj_config.rb

Overview

Add Runtime/local functions

Direct Known Subclasses

Account

Instance Method Summary collapse

Constructor Details

#initialize(config_name = nil) ⇒ Config

Load yaml documents (defaults + config) If config doesn’t exist, it will be created, empty with ‘defaults:’ only

  • Args :

    • config_name : Config file name to use. By default, file path is

      built as #{PrcLib.data_path}/config.yaml
      
  • Returns : -

  • Raises :

    • ++ ->



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lorj_config.rb', line 110

def initialize(config_name = nil)
  config_layers = []

  # Application layer
  config_layers << define_default_layer

  # runtime Config layer
  config_layers << define_controller_data_layer

  # Local Config layer
  local = define_local_layer
  config_layers << local

  # runtime Config layer
  config_layers << define_runtime_layer

  if PrcLib.data_path.nil?
    PrcLib.fatal(1, 'Internal PrcLib.data_path was not set.')
  end

  initialize_local(local[:config], config_name)

  initialize_layers(config_layers)
end

Instance Method Details

#[](key, default = nil) ⇒ Object

Call get function

  • Args :

    • key : key name

    • default: Default value to set if not found.

  • Returns : value found or default

  • Raises : nothing



247
248
249
250
# File 'lib/lorj_config.rb', line 247

def [](key, default = nil) # Re-define PRC::CoreConfig []= function
  return p_get(:keys => [key]) if exist?(key)
  default
end

#config_dump(names = %w(local default)) ⇒ Object

Basic dump

  • Args :

  • Returns :

    • hash of config hashes.

  • Raises : nothing



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lorj_config.rb', line 80

def config_dump(names = %w(local default))
  # Build a config hash.

  res = {}

  names = %w(local default) unless names.is_a?(Array)

  options = _common_options_get(:names => names)
  config_layers = options[0][0]
  if names.length == 1
    res = config_layers[0][:config].data
  else
    config_layers.each do |layer|
      res[layer[:name]] = layer[:config].data
    end
  end
  res
end

#config_filename(name = 'local') ⇒ Object

This function return the filename of the config layer asked:

  • Args :

  • layer_name : Layer name to get the config file name



64
65
66
67
68
69
70
# File 'lib/lorj_config.rb', line 64

def config_filename(name = 'local')
  index =  layer_index(name)

  index = 1 if index.nil?

  @config_layers[index][:config].filename
end

#define_controller_data_layerObject



148
149
150
# File 'lib/lorj_config.rb', line 148

def define_controller_data_layer
  PRC::CoreConfig.define_layer :name => 'controller'
end

#define_default_layerObject



135
136
137
138
139
# File 'lib/lorj_config.rb', line 135

def define_default_layer
  PRC::CoreConfig.define_layer(:name => 'default',
                               :config => Lorj.defaults,
                               :set => false, :load => true)
end

#define_local_layer(latest_version = nil) ⇒ Object



141
142
143
144
145
146
# File 'lib/lorj_config.rb', line 141

def define_local_layer(latest_version = nil)
  PRC::CoreConfig.define_layer(:name => 'local',
                               :config => \
                                PRC::SectionConfig.new(nil, latest_version),
                               :load => true, :save => true)
end

#define_runtime_layerObject



152
153
154
# File 'lib/lorj_config.rb', line 152

def define_runtime_layer
  PRC::CoreConfig.define_layer
end

#fatal_if_inexistent(key) ⇒ Object

Function to return in fatal error if a config data is nil. Help to control function requirement.

  • Args :

    • key : Symbol/String(converted to symbol) key name to test.

  • Returns : nothing

  • Raises :

    • fatal : Call to PrcLib.fatal to exit the application with error 1.



443
444
445
446
# File 'lib/lorj_config.rb', line 443

def fatal_if_inexistent(key)
  PrcLib.fatal(1, "Internal error - %s: '%s' is missing",
               caller, key) unless get(key)
end

#get(key, default = nil) ⇒ Object

Get function Will search over several places:

  • runtime - Call internal runtime_get -

  • local config (config>yaml) - Call internal local_get -

  • application default (defaults.yaml) - Call Lorj.defaults.get -

  • default

key can be an array, a string (converted to a symbol) or a symbol.

  • Args :

    • key : key name

    • default: Default value to set if not found.

  • Returns : value found or default

  • Raises : nothing



234
235
236
# File 'lib/lorj_config.rb', line 234

def get(key, default = nil)
  self[key, default]
end

#get_section(key) ⇒ Object

get_section helps to identify in which section the data is defined by data model of the application.

  • Args :

    • key : key name

  • Returns :

    • the section name found

    OR

    • nil

  • Raises : nothing



265
266
267
268
269
270
271
272
273
274
# File 'lib/lorj_config.rb', line 265

def get_section(key)
  section = Lorj.defaults.get_meta_section(key)

  unless section
    return PrcLib.debug('%s: Unable to get account data '\
                        "'%s'. No section found. check defaults.yaml.",
                        __callee__, key)
  end
  section
end

#initialize_local(config, config_name = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/lorj_config.rb', line 156

def initialize_local(config, config_name = nil)
  config_name = initialize_local_filename(config_name)

  PrcLib.ensure_dir_exists(File.dirname(config_name))

  if File.exist?(config_name)
    config.load(config_name)

    if config.data.rh_key_to_symbol?(2)
      config.rh_key_to_symbol(2)
      config.save config_name
    end

  else
    config.data[:default] =  nil
    # Write the empty file
    PrcLib.info('Creating your default configuration file ...')
    config.save config_name
  end
end

#initialize_local_filename(config_name = nil) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/lorj_config.rb', line 177

def initialize_local_filename(config_name = nil)
  config_default_name = 'config.yaml'

  config_name = nil unless config_name.is_a?(String)
  if config_name
    if File.dirname(config_name) == '.'
      config_name = File.join(PrcLib.data_path, config_name)
    end
    config_name = File.expand_path(config_name)
    unless File.exist?(config_name)
      PrcLib.warning("Config file '%s' doesn't exists. Using default one.",
                     config_name)
      config_name = File.join(PrcLib.data_path, config_default_name)
    end
  else
    config_name = File.join(PrcLib.data_path, config_default_name)
  end
  config_name
end

#local_default_exist?(key) ⇒ Boolean

Function to check default keys existence(in section :default) from local config file only.

  • Args :

    • key : Symbol/String(converted to symbol) key name to test.

  • Returns : -

  • Raises : nothing

Returns:

  • (Boolean)


344
345
346
# File 'lib/lorj_config.rb', line 344

def local_default_exist?(key)
  local_exist?(key)
end

#local_del(key, section = :default) ⇒ Object

Function to Delete a key value in local config file only.

  • Args :

    • key : Symbol/String(converted to symbol) key name to test.

    • section : Section name to test the key.

  • Returns :

    • value deleted

    OR

    • false

  • Raises : nothing



422
423
424
425
426
427
428
429
430
431
# File 'lib/lorj_config.rb', line 422

def local_del(key, section = :default)
  key = key.to_sym if key.class == String

  return false if key.nil?

  index = layer_index('local')
  config = @config_layers[index][:config]
  config.data_options(:section => section)
  config.del(key)
end

#local_exist?(key, section = :default) ⇒ Boolean

Function to check key existence from local config file only.

  • Args :

    • key : Symbol/String(converted to symbol) key name to test.

    • section : Section name to test the key.

  • Returns : -

  • Raises : nothing

Returns:

  • (Boolean)


357
358
359
360
361
362
363
364
# File 'lib/lorj_config.rb', line 357

def local_exist?(key, section = :default)
  index = layer_index('local')

  config = @config_layers[index][:config]
  config.data_options(:section => section)

  config.exist?(key)
end

#local_get(key, section = :default, default = nil) ⇒ Object

Function to Get a key value from local config file only.

  • Args :

    • key : Symbol/String(converted to symbol) key name to test.

    • section : Section name to test the key.

    • default : default value if not found.

  • Returns :

    • Value get or default.

  • Raises : nothing



399
400
401
402
403
404
405
406
407
408
# File 'lib/lorj_config.rb', line 399

def local_get(key, section = :default, default = nil)
  key = key.to_sym if key.class == String

  return default unless local_exist?(key, section)

  index = layer_index('local')
  config = @config_layers[index][:config]
  config.data_options(:section => section)
  config[key]
end

#local_set(key, value, section = :default) ⇒ Object

Function to set a key value in local config file only.

  • Args :

    • key : Symbol/String(converted to symbol) key name to test.

    • value : Value to set

    • section : Section name to test the key.

  • Returns :

    • Value set.

  • Raises : nothing



377
378
379
380
381
382
383
384
385
386
# File 'lib/lorj_config.rb', line 377

def local_set(key, value, section = :default)
  key = key.to_sym if key.class == String
  return false if !key || !value

  index = layer_index('local')

  config = @config_layers[index][:config]
  config.data_options(:section => section)
  config[key] = value
end

#meta_eachObject

each loop on Application Account section/key (meta data). This loop will extract data from :section of the application definition (defaults.yaml) key identified as account exclusive (:account_exclusive = true) are not selected.

  • Args :

    • ++ ->

  • Returns : -

  • Raises :

    • ++ ->



460
461
462
463
464
465
# File 'lib/lorj_config.rb', line 460

def meta_each
  Lorj.defaults.meta_each do |section, key, value|
    next if !value.nil? && value.rh_get(:account_exclusive).is_a?(TrueClass)
    yield section, key, value
  end
end

#runtime_exist?(key) ⇒ Boolean

Check if the key exist as a runtime data.

  • Args :

    • key : key name. It do not support it to be a key tree

      (Arrays of keys).
      
  • Returns :

    • true/false

  • Raises : Nothing

Returns:

  • (Boolean)


288
289
290
291
# File 'lib/lorj_config.rb', line 288

def runtime_exist?(key)
  index = layer_index('runtime')
  @config_layers[index][:config].exist?(key)
end

#runtime_get(key) ⇒ Object

Get exclusively the Runtime data. Internally used by get.

  • Args :

    • key : key name. It do not support it to be a key tree

      (Arrays of keys).
      
  • Returns :

    • key value.

  • Raises : Nothing



303
304
305
306
# File 'lib/lorj_config.rb', line 303

def runtime_get(key)
  index = layer_index('runtime')
  @config_layers[index][:config][key]
end

#save_local_configObject

Save the config.yaml file.

  • Args : nothing

  • Returns :

    • true/false

  • Raises : nothing



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/lorj_config.rb', line 316

def save_local_config
  index = layer_index('local')
  file = file(nil, :index => index)
  begin
    result = save(:index => index)
  rescue => e
    PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
    return false
  end

  if result
    PrcLib.info('Configuration file "%s" updated.', file)
    return true
  end
  PrcLib.debug('Configuration file "%s" was NOT updated.', file)
  false
end

#set(key, value, options = {}) ⇒ Object

Function to set a runtime key/value, but remove it if value is nil. To set in config.yaml, use Lorj::Config::local_set To set on extra data, like account information, use Lorj::Config::extra_set

  • Args :

    • key : key name. Can be an key tree (Array of keys).

    • value : Value to set

    • options : possible options:

      • :name : layer to exclusively set data.

      • :index : layer index to exclusively set data. If neither :name or :index is set, set will use the ‘runtime’ layer.

  • Returns :

    • value set

  • Raises : Nothing



214
215
216
# File 'lib/lorj_config.rb', line 214

def set(key, value, options = {})
  p_set(options.merge(:keys => [key], :value => value))
end