Class: Lorj::Account

Inherits:
Config show all
Defined in:
lib/lorj_account.rb,
lib/lorj_account.rb,
lib/lorj_account.rb

Overview

Defines internal functions

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Config

#config_dump, #config_filename, #define_controller_data_layer, #define_default_layer, #define_local_layer, #define_runtime_layer, #fatal_if_inexistent, #get_section, #initialize_local, #initialize_local_filename, #local_default_exist?, #local_del, #local_exist?, #local_get, #local_set, #meta_each, #runtime_exist?, #runtime_get, #save_local_config

Constructor Details

#initialize(config_name = nil, latest_version = nil) ⇒ Account

Lorj::Account implements Config layers.

  • default : Represents the application defaults.yaml config.

  • controller : Represents the controller config redefinition. See BaseDefinition::define_data

  • local : Represents the config.yaml located in ~/.forj

  • account : Represents an Account data located in ~/.forj/accounts

  • runtime : Represents the runtime in memory data settings.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/lorj_account.rb', line 143

def initialize(config_name = nil, latest_version = 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(latest_version)
  config_layers << local

  # Account config layer
  config_layers << (latest_version)

  # 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 Attribute Details

#account_nameObject (readonly)

Returns the value of attribute account_name.



132
133
134
# File 'lib/lorj_account.rb', line 132

def 
  @account_name
end

Instance Method Details

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

Simple get call with default options Alternative is to use Account::get



223
224
225
# File 'lib/lorj_account.rb', line 223

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

#[]=(key, value) ⇒ Object

Set a key to te runtime config layer. Alternative is to use Account::set



412
413
414
# File 'lib/lorj_account.rb', line 412

def []=(key, value)
  set(key, value)
end

#ac_eraseObject



481
482
483
484
485
486
487
# File 'lib/lorj_account.rb', line 481

def ac_erase
  index = layer_index('account')

   = @config_layers[index][:config]
  .erase
  true
end

#ac_load(sAccountName = @account_name) ⇒ Object

Load Account Information



490
491
492
493
494
495
496
497
498
499
500
# File 'lib/lorj_account.rb', line 490

def ac_load(sAccountName = @account_name)
  @account_name = sAccountName unless !sAccountName.nil? &&
                                      sAccountName == @account_name
  return false if @account_name.nil?

   = File.join(@account_path, @account_name)
  return false unless File.exist?()
  index = layer_index('account')

  _do_load(@config_layers[index][:config], )
end

#ac_new(sAccountName, provider_name) ⇒ Object



463
464
465
466
467
# File 'lib/lorj_account.rb', line 463

def ac_new(sAccountName, provider_name)
  return nil if sAccountName.nil?
  ac_erase
  ac_update(sAccountName, provider_name)
end

#ac_saveObject

Account save function. Use set/get to manage those data that you will be able to save in an account file.

  • Args :

  • Returns :

  • true if saved

OR

  • false if:

    • the account do not set the :provider name.

    • value is nil. The value is set to nil, then.

OR

  • nil if:

    • account_name is not set

  • Raises : Nothing



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/lorj_account.rb', line 519

def ac_save
  return nil if @account_name.nil?

   = File.join(@account_path, @account_name)

  local_index = layer_index('local')
   = layer_index('account')

   = @config_layers[][:config]
  local = @config_layers[local_index][:config]

  .data_options(:section => :account)

  [:provider].each do |key|
    next if .exist?(key) && ![key].nil?

    PrcLib.error "':%s' is not set. Unable to save the account '"\
                 "%s' to '%s'", key.to_s, @account_name, 
    return false
  end

  result = local.save

  return result unless result

  .data_options(:section => :account)
  [:name] = @account_name
  .filename = 
  .save

  true
end

#ac_update(sAccountName, provider_name) ⇒ Object

update Account protected data account name and provider name.



472
473
474
475
476
477
478
479
# File 'lib/lorj_account.rb', line 472

def ac_update(sAccountName, provider_name)
  return nil if sAccountName.nil?
  @account_name = sAccountName
  index = layer_index('account')

   = @config_layers[index][:config]
  .ac_new sAccountName, provider_name
end

#del(key, options = {}) ⇒ Object

This function delete a section/key. If no section is defined, it will assume to be :default. Without any options, the runtime layer is used to delete the key.

  • Args :

    • key : key name. It do not support it to be a key tree (Arrays of keys).

    • value : value to set

    • options : possible options:

      • :section : Force to use a specific section name.

      • :name : layer to exclusively get data.

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

  • Returns :

    • the value set

    OR

    • nil if:

      • lorj data model set this key as readonly.

      • value is nil. The value is set to nil, then.

      • key is nil. No update is done.

  • Raises : Nothing



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/lorj_account.rb', line 440

def del(key, options = {})
  parameters = validate_key_and_options(key, options)
  return nil if parameters.nil?

  key = parameters[0][0]
  layer_name, section = parameters[1]

  section = Lorj.defaults.get_meta_section(key) if section.nil?
  section = :default if section.nil?
  section, key = _detect_section(key, section)

  return nil if readonly?(key, section)

  options = { :keys => [key], :section => section }

  options[:index] = index_to_update(layer_name, key, section)

  p_del(options)
end

#exclusive?(key, section = nil) ⇒ Boolean

Return true if exclusive set won’t be able to update this value. Only p_set (private function) is able.

  • Args :

    • key : key name. It can support it to be a key tree (Arrays of keys).

    • section : optionnal. If missing the section name is determined by the data name associated

  • Returns :

    • true/false : readonly value

    OR

    • nil if:

      • section was not found

Returns:

  • (Boolean)


352
353
354
355
356
357
358
359
360
361
362
# File 'lib/lorj_account.rb', line 352

def exclusive?(key, section = nil)
  return nil unless key

  key = key.to_sym if key.class == String
  section, key = Lorj.data.first_section(key) if section.nil?

  return nil if section.nil?
  result = Lorj.data[:sections, section, key, :account_exclusive]
  return result if result.boolean?
  result
end

#exist?(key, options = nil) ⇒ Boolean

check key/value existence in config layers

  • Args :

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

    keys).

    • options : possible options:

      • :section : Force to use a specific section name.

      • :names : array of layers name to exclusively get data.

      • :name : layer name to exclusively get data.

      • :indexes : array of layers index to exclusively get data.

      • :index : layer index to exclusively get data. If neither :name or :index is set, get will search data on all predefined layers, first found.

  • Returns :

    • true : if found in runtime.

    • true : if found in the Account data structure.

    • true : if found in the local configuration file. Usually ~/.forj/config.yaml

    • true : if found in the Application default (File ‘defaults.yaml’) (class Default)

    • false otherwise.

  • Raises : Nothing

Returns:

  • (Boolean)


292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/lorj_account.rb', line 292

def exist?(key, options = nil)
  key = key.to_sym if key.class == String
  options = {} unless options.is_a?(Hash)

  section = options[:section]
  section, key = Lorj.data.first_section(key) if section.nil?
  options = options.merge(:keys => [key])
  options.delete(:section)

  indexes = _identify_indexes(options, exclusive?(key, section))

  names = []
  indexes.each { |index| names << @config_layers[index][:name] }

  options[:data_options] = _set_data_options_per_names(names, section)

  p_exist?(options)
end

#get(key, default = nil, options = {}) ⇒ Object

get function. If the Application meta data option of the key is set with :account_exclusive => true, get will limit to runtime then account. otherwise, search in all layers.

The data found is parse through ERB with self as context.

  • Args :

    • key : key name. It do not support it to be a key tree (Arrays of keys).

    • default : default value, if not found.

    • options : Options for get:

      • :section : Get will use this section name instead of searching it.

      • :names : array of layers name to exclusively get data.

      • :name : layer name to exclusively get data.

      • :indexes : array of layers index to exclusively get data.

      • :index : layer index to exclusively get data. If neither :name or :index is set, get will search data on all predefined layers, first found.

  • Returns :

    • key value.

  • Raises : Nothing



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/lorj_account.rb', line 196

def get(key, default = nil, options = {})
  key = key.to_sym if key.class == String
  options = {} unless options.is_a?(Hash)

  section = options[:section]
  section, key = Lorj.data.first_section(key) if section.nil?

  options = options.merge(:keys => [key])
  options.delete(:section)

  indexes = _identify_indexes(options, exclusive?(key, section))

  names = []
  indexes.each { |index| names << @config_layers[index][:name] }

  options[:data_options] = _set_data_options_per_names(names, section)

  if p_exist?(options)
    value = p_get(options)
    return value unless value.is_a?(String)
    return ERB.new(value).result ERBConfig.new(self).get_binding
  end
  default
end

#meta_type?(key) ⇒ Boolean

Returns:

  • (Boolean)


558
559
560
561
562
563
564
565
# File 'lib/lorj_account.rb', line 558

def meta_type?(key)
  return nil unless key

  section = Lorj.defaults.get_meta_section(key)

  return section if section == :default
  @account_name
end

#readonly?(key, section = nil) ⇒ Boolean

Return true if readonly. set won’t be able to update this value. Only p_set (private function) is able.

  • Args :

    • key : key name. It can support it to be a key tree (Arrays of keys).

    • section : optionnal. If missing the section name is determined by the data name associated

  • Returns :

    • true/false : readonly value

    OR

    • nil if:

      • section was not found

Returns:

  • (Boolean)


324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/lorj_account.rb', line 324

def readonly?(key, section = nil)
  return nil unless key

  key = key.to_sym if key.class == String
  section = Lorj.defaults.get_meta_section(key) if section.nil?
  section, key = _detect_section(key, section)

  return nil if section.nil?

  result = Lorj.data.section_data(section, key, :readonly)
  return result if result.boolean?
  false
end

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

This function update a section/key=value if the account structure is defined. (see Lorj::Defaults) If no section is defined, it will assume to be :default.

  • Args :

    • key : key name. It do not support it to be a key tree (Arrays of keys).

    • value : value to set

    • options : possible options:

      • :section : Force to use a specific section name.

      • :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 :

    • the value set

    OR

    • nil if:

      • lorj data model set this key as readonly.

      • value is nil. The value is set to nil, then.

      • key is nil. No update is done.

  • Raises : Nothing



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/lorj_account.rb', line 388

def set(key, value, options = {})
  options[:name] = 'runtime' unless options.key?(:name)

  parameters = validate_key_and_options(key, options)
  return nil if parameters.nil?

  key = parameters[0][0]
  layer_name, section = parameters[1][0]

  found_section, key = Lorj.data.first_section(key)
  section = found_section if section.nil?
  section = :default if section.nil?

  return nil if readonly?(key, section)

  options = { :keys => [key], :section => section, :value => value }

  options[:index] = index_to_update(layer_name, key, section)

  p_set(options)
end

#where?(key, options = {}) ⇒ Boolean

where? function. If the Application meta data option of the key is set with :account_exclusive => true, get will limit to runtime then account. otherwise, search in all layers.

  • Args :

    • key : key name. It do not support it to be a key tree (Arrays of keys).

    • options : possible options:

      • :section : Force to use a specific section name.

      • :names : array of layers name to exclusively get data.

      • :name : layer name to exclusively get data.

      • :indexes : array of layers index to exclusively get data.

      • :index : layer index to exclusively get data. If neither :name or :index is set, get will search data on all predefined layers, first found, first listed.

  • Returns :

    • config name found.

  • Raises : Nothing

Returns:

  • (Boolean)


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/lorj_account.rb', line 247

def where?(key, options = {})
  key = key.to_sym if key.class == String
  options = {} unless options.is_a?(Hash)

  section = options[:section]
  section, key = Lorj.data.first_section(key) if section.nil?

  indexes = _identify_indexes(options, exclusive?(key, section))

  names = []
  indexes.each { |index| names << @config_layers[index][:name] }

  where_options = {
    :keys => [key],
    :indexes => indexes,
    :data_options => _set_data_options_per_names(names, section)
  }

  p_where?(where_options)
end