Class: Lorj::Core

Inherits:
Object show all
Defined in:
lib/core/core.rb,
lib/core/core_internal.rb,
lib/core/core_internal.rb,
lib/core/core_internal.rb

Overview

Define internal private functions for controllers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(the_config = nil, processes = nil, controller_class = nil) ⇒ Core

Core parameters are: the_config : Optional. An instance of a configuration system which HAVE to provide get/set/exist?/[]/=

  • Args:

  • Processes: Array of processes with controller This array, contains a list of process to load and optionnaly a controller.

    You can define your own process or a process module. The array is structured as follow:

    • each element contains a Hash with: If you are using a process module, set the following:

      • :process_module : Name of the process module to load

      If you are not using a Process module, you need to set the following:

      • :process_path : Path to a local process code. This path must contains at least ‘process’ subdir. And if needed a ‘controllers’ path

      • :process_name : Name of the local process

      Optionnally, you can set a controller name to use with the process.

      • :controller_name: Name of the controller to use.

      • :controller_path: Path to the controller file.



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/core/core.rb', line 378

def initialize(the_config = nil, processes = nil, controller_class = nil)
  # Loading ProcessClass
  # Create Process derived from respectively BaseProcess
  PrcLib.core_level = 0 if PrcLib.core_level.nil?

  init_core_config(the_config)

  PrcLib.model.config = @config

  model = initialize_model

  # Compatibility with old 'new syntax'
  # `processes` will get an Array of string/symbol or simply a string/symbol
  # `controller_class` is used to define the controller to load.
  # string/symbol
  processes = adapt_core_parameters(processes, controller_class)

  # Load Application processes
  init_processes(model, processes)

  PrcLib.runtime_fail 'Lorj::Core: No valid process loaded. '\
                       'Aborting.' if model[:process_class].nil?

  # Create Core object with the application model loaded
  # (processes & controller)
  initialize_core_object(model)
  PrcLib.model.clear_heap
end

Instance Attribute Details

#configObject (readonly)

Public access to a config object. A config object can be any kind of class which should provide at least following functions:

  • get(*key, default=nil) and [*key] : function to get a value from a key. default is a value to get if not found.

  • set(*key, value) or [*key, value]= : function to set a value to a key. Ex: From processes, you can set a runtime data with:

    config.set(key, value)
    

    OR

    config[key] = value
    
  • exist?(*key) : function which return false if not found, or any other value if found. Ex: From processes, you can get a data (runtime/account/config.yaml or defaults.yaml) with:

    config.get(key)
    

    OR

    config[key]
    

For each functions, *key is a list of value, which becomes an array in the function. It should accept to manage the key tree (hash of hashes)

Currently lorj comes with Lorj::Config or Lorj::Account. Thoses classes defines at least those 5 functions. And more.



112
113
114
# File 'lib/core/core.rb', line 112

def config
  @config
end

Instance Method Details

#_process_load(model, my_process) ⇒ Object

high level function to load process



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/core/core_internal.rb', line 258

def _process_load(model, my_process)
  if load_process(model, my_process[:process_path])
    controller_class = my_process[:controller_path]
    controller_class = my_process[:controller_name] if controller_class.nil?

    init_controller(model, controller_class) if controller_class
  else
    PrcLib.warning("Process '%s' not properly loaded.",
                   my_process[:process_path])
  end
end

#_process_load_data(config, index, name, file, config_class = PRC::BaseConfig) ⇒ Object

Load data definition in process data layers



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/core/core_internal.rb', line 356

def _process_load_data(config, index, name, file,
                       config_class = PRC::BaseConfig)
  return unless config.layer_index(name).nil?

  return if file.nil?

  layer = PRC::CoreConfig.define_layer(:name => name,
                                       :config => config_class.new,
                                       :load => true,
                                       :set => false)
  unless File.exist?(file)
    PrcLib.warning("Process '%s', data file '%s' doesn't exist."\
                   ' Not loaded.', name, file)
    return
  end

  begin
    unless layer[:config].load(file)
      PrcLib.warning("Process '%s', data file '%s' was not loaded.",
                     name, file)
    end
  rescue => e
    PrcLib.error("Process '%s', unable to load data file '%s'. %s",
                 name, file, e.message)
  end

  config.layer_add(layer.merge(:index => (config.layers.length - index)))
end

#_process_local_to_load(index, my_process, a_process) ⇒ Object

function prepare of loading a local process



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/core/core_internal.rb', line 271

def _process_local_to_load(index, my_process, a_process)
  my_process[:process_path] = a_process[:process_path]

  if a_process[:process_name].nil?
    my_process[:process_name] = File.basename(a_process[:process_path])
  else
    my_process[:process_name] = a_process[:process_name]
  end

  if a_process[:controller_path]
    my_process[:controller_path] = a_process[:controller_path]
  else
    my_process[:controller_name] = a_process[:controller_name]
  end

  defaults_file = a_process[:defaults]
  if defaults_file.nil?
    path = a_process[:process_path]
    path['.rb'] = ''
    defaults_file = File.join(path, 'defaults.yaml')
  end

  _process_load_data(config, index,
                     a_process[:process_name], defaults_file,
                     PRC::SectionConfig)

  data_file = a_process[:data]
  if defaults_file.nil?
    path = a_process[:process_path]
    path['.rb'] = ''
    data_file = File.join(path, 'data.yaml')
  end

  _process_load_data(Lorj.data, index,
                     a_process[:process_name], data_file)

  # TODO: Implement Object definition as a config layer.
  # _process_load_definition(definition, index, a_process[:process_name],
  #                          a_process[:definition])

  my_process
end

#_process_module_set_ctr(my_process, controllers, controller_name) ⇒ Object

Load process definition in process layers Function currently not developped.

def _process_load_definition(a_process) end



391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/core/core_internal.rb', line 391

def _process_module_set_ctr(my_process, controllers, controller_name)
  return if controller_name.nil?

  controller_path = controllers[controller_name]

  if controller_path.nil?
    PrcLib.warning("Controller '%s' was not found. Please check. The "\
                   'process may not work.', controller_name)
    return
  end

  my_process[:controller_path] = controller_path
end

#_process_module_to_load(index, my_process, a_process) ⇒ Object

Function prepare of loading a module process.



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/core/core_internal.rb', line 315

def _process_module_to_load(index, my_process, a_process)
  name = a_process[:process_module]

  if name.nil?
    PrcLib.warning(':process_module is empty. Process not properly loaded.')
    return
  end

  name = name.to_s if name.is_a?(Symbol)

  unless Lorj.processes.key?(name)
    PrcLib.warning("Unable to find Process module '%s'. Process not "\
                   'properly loaded.', name)
    return
  end

  module_process = Lorj.processes[name]
  my_process[:process_name] = name
  my_process[:process_path] = module_process.process

  if a_process[:controller_path]
    my_process[:controller_path] = a_process[:controller_path]
    return my_process
  end

  _process_module_set_ctr(my_process, module_process.controllers,
                          a_process[:controller_name])

  _process_load_data(config, index, name, module_process.defaults_file,
                     PRC::SectionConfig)

  _process_load_data(Lorj.data, index, name, module_process.data_file)

  # TODO: Implement Object definition as a config layer.
  # _process_load_definition(definition, index, name,
  #                          module_process.definition)

  my_process
end

#account_export(map = nil, with_name = true) ⇒ Object

Function to export a Lorj Account in an encrypted Hash.

For details about this functions, see #Lorj::BaseDefinition.account_export

  • Args :

    • map : Hash map of fields to extract. if map is nil, the export function will loop in the list of keys in the ‘account’ layer.

    • with_name : True to extract :name and :provider as well. True by default.

  • returns :

    • key: String. Key used to encrypt.

    • env_hash: String. Base64 encrypted Hash.

    OR

    • nil if issues.



348
349
350
351
# File 'lib/core/core.rb', line 348

def (map = nil, with_name = true)
  return nil if @core_object.nil?
  @core_object.(map, with_name)
end

#account_import(key, enc_hash, name = nil, controller = nil) ⇒ Object

Function to import an encrypted Hash as a Lorj Account.

For details about this functions, see #Lorj::BaseDefinition.account_import

  • Args :

    • key : key to use to decrypt the ‘enc_hash’.

    • enc_hash : Encrypted Hash.

  • Raises : No exceptions



328
329
330
331
# File 'lib/core/core.rb', line 328

def (key, enc_hash, name = nil, controller = nil)
  return nil if @core_object.nil?
  @core_object.(key, enc_hash, name, controller)
end

#classname_from_file(file) ⇒ Object

Function which determine the class name from the file name. rules:

  • First character : Capitalized

  • Any character prefixed by ‘_’ : capitalized. ‘_’ is removed.

  • Args :

    • the_process_file : Process file to analyze.

  • Returns :

    • ProcessClassName : string representing the name of the class.



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/core/core_internal.rb', line 459

def classname_from_file(file)
  the_process_class = File.basename(file)

  the_process_class['.rb'] = '' if the_process_class['.rb']

  if (/[A-Z]/ =~ the_process_class) != 0
    the_process_class = the_process_class.capitalize
  end

  match_found = the_process_class.scan(/_[a-z]/)
  if match_found
    # Ruby 1.8   : 'ab'[1] => 98 and 'ab'[1, 1] => 'b'
    # Ruby 1.9 + : 'ab'[1] => 'b' and 'ab'[1, 1] => 'b'
    match_found.each { |str| the_process_class[str] = str[1, 1].capitalize }
  end

  the_process_class
end

#connect(oCloudObj, hConfig = nil) ⇒ Object

a wrapper to Create call. Use this function for code readibility.

  • Args :

    • oCloudObj : Name of the object to initialize.

    • hConfig : Hash of hashes containing data required to initialize

      the object.
      

      If you use this variable, any other runtime config defined by the Data model will be cleaned before

  • Returns :

    • Lorj::Data : Represents the Object initialized.

  • Raises : No exceptions



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

def connect(oCloudObj, hConfig = nil)
  return nil if !oCloudObj || !@core_object
  @core_object.process_create(oCloudObj, hConfig)
end

#create(oCloudObj, hConfig = nil) ⇒ Object

Execute the creation process to create the object oCloudObj. The creation process can add any kind of complexity to get the a memory representation of the object manipulated during creation process. This means that a creation process can be (non exhaustive list of possibilities)

  • a connection initialization

  • an internal memory data structure, like hash, array, ruby object…

  • a get or create logic

  • Args :

    • oCloudObj : Name of the object to initialize.

    • hConfig : Hash of hashes containing data required to initialize

      the object.
      

      If you use this variable, any other runtime config defined by the Data model will be cleaned before

  • Returns :

    • Lorj::Data : Represents the Object initialized.

  • Raises : No exceptions



157
158
159
160
# File 'lib/core/core.rb', line 157

def create(oCloudObj, hConfig = nil)
  return nil if !oCloudObj || !@core_object
  @core_object.process_create(oCloudObj, hConfig)
end

#delete(oCloudObj, hConfig = nil) ⇒ Object

Execution of the delete process for the oCloudObj object. It requires the object to be loaded in lorj Lorj::Data objects cache. You can use Create or Get functions to load this object.

  • Args :

    • oCloudObj : Name of the object to initialize.

    • hConfig : Hash of hashes containing data required to initialize

      the object.
      

      If you use this variable, any other runtime config defined by the Data model will be cleaned before

  • Returns :

    • Lorj::Data : Represents the Object initialized.

  • Raises : No exceptions



199
200
201
202
203
# File 'lib/core/core.rb', line 199

def delete(oCloudObj, hConfig = nil)
  return nil if !oCloudObj || !@core_object

  @core_object.process_delete(oCloudObj, hConfig)
end

#get(oCloudObj, sId, hConfig = nil) ⇒ Object

Execution of the Get process for the oCloudObj object.

  • Args :

    • oCloudObj : Name of the object to initialize.

    • sId : data representing the ID (attribute :id) of a Lorj::Data

      object.
      
    • hConfig : Hash of hashes containing data required to initialize

      the object.
      

      If you use this variable, any other runtime config defined by the Data model will be cleaned before

  • Returns :

    • Lorj::Data : Represents the Object initialized.

  • Raises : No exceptions



244
245
246
247
248
# File 'lib/core/core.rb', line 244

def get(oCloudObj, sId, hConfig = nil)
  return nil if !oCloudObj || !@core_object || sId.nil?

  @core_object.process_get(oCloudObj, sId, hConfig)
end

#get_or_create(oCloudObj, hConfig = nil) ⇒ Object

a wrapper to Create call. Use this function for code readibility.

  • Args :

    • oCloudObj : Name of the object to initialize.

    • hConfig : Hash of hashes containing data required to initialize

      the object.
      

      If you use this variable, any other runtime config defined by the Data model will be cleaned before

  • Returns :

    • Lorj::Data : Represents the Object initialized.

  • Raises : No exceptions



177
178
179
180
# File 'lib/core/core.rb', line 177

def get_or_create(oCloudObj, hConfig = nil)
  return nil if !oCloudObj || !@core_object
  @core_object.process_create(oCloudObj, hConfig)
end

#load_process(model, the_process) ⇒ Object

Function to load a process.

  • Args :

    • the_process_ : Process to load. Can be a string or

      a path to a file
      
  • Returns :

    • load_status : true if loaded, false otherwise



434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/core/core_internal.rb', line 434

def load_process(model, the_process)
  Lorj.debug(1, "Loading Process '%s'", the_process)

  if the_process.include?('/')
    file = File.expand_path(the_process)
  else
    file = processfile_from_default(model, the_process)
  end

  return PrcLib.warning("Process file definition '%s' is missing. ",
                        file) unless File.exist?(file)

  load_processfile(model, file, classname_from_file(file))
end

#load_processfile(model, file, the_process_class) ⇒ Object

Load a process file in ruby.

  • Args :

    • file : Process file to load.

  • Returns :

    • loaded : load status



498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/core/core_internal.rb', line 498

def load_processfile(model, file, the_process_class)
  model[:process_class] = BaseProcess if model[:process_class].nil?

  new_class = Class.new(model[:process_class])
  unless /Process$/ =~ the_process_class
    the_process_class = format('%sProcess',  the_process_class)
  end

  Lorj.debug(1, "Declaring Process '%s'", the_process_class)
  model[:process_class] = Object.const_set(the_process_class, new_class)
  model[:process_class_name] = the_process_class
  BaseDefinition.current_process(model[:process_class])
  load file
end

#processes_as_array(processes_parameter) ⇒ Object

Function analyzing the process class parameter and return the list of processes in an array of processes.

  • Args :

    • processes_parameter : Parameter to interpret. supports:

      - nil => return []
      - String/Symbol => return [String/Symbol]
      - Array => return Array
      
  • Returns :

    • array_processes : Array of processes.



418
419
420
421
422
423
424
# File 'lib/core/core_internal.rb', line 418

def processes_as_array(processes_parameter)
  return [] if processes_parameter.nil?

  return [processes_parameter] unless processes_parameter.is_a?(Array)

  processes_parameter
end

#processfile_from_default(_model, the_process_class) ⇒ Object

Determine the process file path from the single name. Uses PrcLib.process_path as path to load this process.

  • Args :

    • the_process_class : Process to load.

  • Returns :



487
488
489
# File 'lib/core/core_internal.rb', line 487

def processfile_from_default(_model, the_process_class)
  File.join(PrcLib.process_path, the_process_class + '.rb')
end

#query(oCloudObj, sQuery, hConfig = nil) ⇒ Object

Execution of the Query process for the oCloudObj object.

  • Args :

    • oCloudObj : Name of the object to initialize.

    • sQuery : Hash representing the query filter.

    • hConfig : Hash of hashes containing data required to initialize

      the object.
      

      If you use this variable, any other runtime config defined by the Data model will be cleaned before

  • Returns :

    • Lorj::Data : Represents the Object initialized.

  • Raises : No exceptions



221
222
223
224
225
# File 'lib/core/core.rb', line 221

def query(oCloudObj, sQuery, hConfig = nil)
  return nil if !oCloudObj || !@core_object

  @core_object.process_query(oCloudObj, sQuery, hConfig)
end

#register(oObject, sObjectType = nil) ⇒ Object

Function to add an object to Lorj cache.

This function is typically used when a previous query has been executed and you want to keep the object in lorj cache. Lorj cache is used by create/delete/etc… To avoid requerying or creating those objects (+dependencies) if missing.

  • Args :

    • oObject : object to register.

    • sObjectType : Object type to register. By default, the object type is determined by the Object itself if this object is an ObjectData. Otherwise, you need to define the object type.

  • Raises : No exceptions



313
314
315
316
# File 'lib/core/core.rb', line 313

def register(oObject, sObjectType = nil)
  return nil if !oObject || !@core_object
  @core_object.register(oObject, sObjectType)
end

#setup(oCloudObj, _sAccountName = nil) ⇒ Object

Function used to ask users about setting up his account.

  • Args :

    • oCloudObj : Name of the object to initialize.

    • sAccountName : Obsolete. You have to load the account data before

      If you use this variable, any other runtime config defined by the Data model will be cleaned before

  • Returns :

    • Lorj::Data : Represents the Object initialized.

  • Raises : No exceptions



292
293
294
295
# File 'lib/core/core.rb', line 292

def setup(oCloudObj, _sAccountName = nil)
  return nil if !oCloudObj || !@core_object
  @core_object.process_setup(oCloudObj)
end

#update(oCloudObj, hConfig = nil) ⇒ Object

Execution of the Update process for the oCloudObj object. Usually, the Controller object data is updated by the process (BaseController::set_attr) then it should call a controller_update to really update the data in the controller.

  • Args :

    • oCloudObj : Name of the object to initialize.

    • sId : data representing the ID (attribute :id) of a Lorj::Data

      object.
      
    • hConfig : Hash of hashes containing data required to initialize

      the object.
      

      If you use this variable, any other runtime config defined by the Data model will be cleaned before

  • Returns :

    • Lorj::Data : Represents the Object initialized.

  • Raises : No exceptions



271
272
273
274
275
# File 'lib/core/core.rb', line 271

def update(oCloudObj, hConfig = nil)
  return nil if !oCloudObj || !@core_object

  @core_object.process_update(oCloudObj, hConfig)
end