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,
lib/core/core_internal.rb

Overview

Define private Initialize 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.



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/core/core.rb', line 410

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
  PrcLib.processes model[:processes]
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.



110
111
112
# File 'lib/core/core.rb', line 110

def config
  @config
end

Instance Method Details

#_process_load(model, my_process) ⇒ Object

high level function to load process



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

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



355
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
# File 'lib/core/core_internal.rb', line 355

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



269
270
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
# File 'lib/core/core_internal.rb', line 269

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



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

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

  unless controller_name.is_a?(String)
    controller_name = controller_name.to_s
  end
  controller_path = controllers[controller_name]

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

  my_process[:controller_path] = controller_path
  my_process[:controller_name] = controller_name
end

#_process_module_to_load(index, my_process, a_process) ⇒ Object

Function prepare of loading a module process.



313
314
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
# File 'lib/core/core_internal.rb', line 313

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
  my_process[:lib_name] = module_process.lib_name

  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, account_only = false, processes_options = {}) ⇒ 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.



378
379
380
381
382
383
# File 'lib/core/core.rb', line 378

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

#account_import(data, name = nil) ⇒ Object

Function to import an encrypted Hash as a Lorj Account.

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

To import an exported data, consider calling Lorj.account_import.

  • Args :

    • data : Hash. data to import.

    • name : By default, it import in the same name described in the data. But we can change it with this parameter.

  • Raises : No exceptions



358
359
360
361
# File 'lib/core/core.rb', line 358

def (data, name = nil)
  return nil if @core_object.nil?
  @core_object.(data, name)
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.



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/core/core_internal.rb', line 463

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



127
128
129
130
# File 'lib/core/core.rb', line 127

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



155
156
157
158
# File 'lib/core/core.rb', line 155

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



197
198
199
200
201
# File 'lib/core/core.rb', line 197

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



242
243
244
245
246
# File 'lib/core/core.rb', line 242

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



175
176
177
178
# File 'lib/core/core.rb', line 175

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



438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/core/core_internal.rb', line 438

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

#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.



422
423
424
425
426
427
428
# File 'lib/core/core_internal.rb', line 422

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

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

  processes_parameter
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



219
220
221
222
223
# File 'lib/core/core.rb', line 219

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

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

#refresh(object) ⇒ 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



296
297
298
299
300
301
# File 'lib/core/core.rb', line 296

def refresh(object)
  return false unless object.is_a?(Lorj::Data) && !object.empty? &&
                      object.type == :object

  @core_object.process_update(object)
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



339
340
341
342
# File 'lib/core/core.rb', line 339

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



318
319
320
321
# File 'lib/core/core.rb', line 318

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



269
270
271
272
273
# File 'lib/core/core.rb', line 269

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

  @core_object.process_update(oCloudObj, hConfig)
end