class Lorj::ObjectData

Represents a list of key/value pairs if the value is a Lorj::Data(data or list), the key will be the Lorj::Data type.

This class is used in 3 different contexts:

Public Class Methods

new(internal = false) click to toggle source

Initialize the object. By default, usage is for controller context.

  • Args :

    • internal : Context

      • true if process context

      • false if controller context. This is the default value.

  • Returns :

    • nothing

  • Raises : No exceptions

# File lib/core/core_object_data.rb, line 85
def initialize(internal = false)
  @params = {}
  @params[:hdata] = {} unless internal
  @internal = internal
  @refresh = nil
end

Public Instance Methods

<<(hHash) click to toggle source

Merge 2 ObjectData.

  • Args :

    • hHash : Hash of Lorj::Data. But it is possible to have different

      object type (not Lorj::Data)
      
  • Returns : hash merged

  • Raises : nothing

# File lib/core/core_object_data.rb, line 190
def <<(hHash)
  @params.merge!(hHash) unless hHash.nil?
end
[](*key) click to toggle source

Get function

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

  • Args :

    • key : key tree (list of keys) If key == :attrs, get will forcelly use the Lorj::Data object attributes If key == :ObjectData, get will forcelly return the controller object otherwise, get will depends on the context:

      • controller context: will return the controller object

      • Process context: will return the Lorj::Data object attributes

  • Returns : value found or nil.

  • Raises : nothing

# File lib/core/core_object_data.rb, line 126
def [](*key)
  key = key.flatten

  return @params if key.length == 0

  object = @params.rh_get(key[0])

  # Return ObjectData, attributes if asked. or depends on context.
  value = object_data_get(object, key)
  # otherwise, simply return what is found in keys hierarchy.
  value = @params.rh_get(key) if value.nil?

  value
end
add(oDataObject) click to toggle source

Add function. Add a Lorj::Data (data or list) to the ObjectData list.

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

  • Args :

  • Returns : Nothing

  • Raises : nothing

# File lib/core/core_object_data.rb, line 151
def add(oDataObject)
  # Requires to be a valid framework object.
  unless oDataObject.is_a?(Lorj::Data)
    PrcLib.runtime_fail "Invalid Framework object type '%s'.",
                        oDataObject.class
  end
  object_data_add(oDataObject)
  oDataObject.register
end
delete(obj) click to toggle source

delete function. delete a Lorj::Data (data or list) from the ObjectData cache.

  • Args :

  • Returns : Nothing

  • Raises : nothing

# File lib/core/core_object_data.rb, line 170
def delete(obj)
  if obj.is_a?(Symbol)
    object_type = obj
    obj = @params[object_type]
    @params.delete(object_type)
  else
    object_data_delete(obj)
  end
  obj.unregister unless obj.nil?
end
exist?(*key) click to toggle source

check Lorj::Data attributes or object exists. Or check key/value pair existence.

  • Args :

    • hHash : Hash of Lorj::Data. But it is possible to have different

      object type (not Lorj::Data)
      
  • Returns : true/false

  • Raises : PrcError

# File lib/core/core_object_data.rb, line 204
def exist?(*key) # rubocop: disable Metrics/MethodLength
  unless [Array, String, Symbol].include?(key.class)
    PrcLib.runtime_fail 'ObjectData: key is not list of values '\
                        '(string/symbol or array)'
  end
  key = [key] if key.is_a?(Symbol) || key.is_a?(String)

  key = key.flatten

  object = @params.rh_get(key[0])
  return false if object.nil?

  if object.is_a?(Lorj::Data)
    object_data_exist?(object, key)
  else
    # By default true if found key hierarchy
    @params.rh_exist?(*key)
  end
end
refresh() click to toggle source
# File lib/core/core_object_data.rb, line 103
def refresh
  return self if @refresh.nil?
  # Do the refresh itself.
  @refresh[:bd_obj].update_params(self, @refresh)
end
refresh_set(base_def_instance, object_type, sEventType, as_controller) click to toggle source

Refresh setting

This function is used to provide capability to an Object to be refreshed from Lorj Cache.

# File lib/core/core_object_data.rb, line 97
def refresh_set(base_def_instance, object_type, sEventType, as_controller)
  @refresh = { :bd_obj => base_def_instance, :object_type => object_type,
               :event_type => sEventType, :controller => as_controller
             }
end
to_s() click to toggle source
# File lib/core/core_object_data.rb, line 245
def to_s
  str = "-- Lorj::ObjectData --\n"
  str += "Usage internal\n" if @internal
  @params.each { |key, data| str += format("%s:\n%s\n", key, data.to_s) }
  str
end
type?(key) click to toggle source

Determine the type of object identified by a key. Lorj::Data attributes or object exists. Or check key/value pair existence.

  • Args :

  • Returns :

    • nil if not found

    • :data if the key value is simply a data

    • :DataObject if the key value is a Lorj::Data

  • Raises : PrcError

# File lib/core/core_object_data.rb, line 236
def type?(key)
  return nil unless @params.rh_exist?(key)
  return :DataObject if @params[key].class == Lorj::Data &&
                        @params[key].type == :object
  :data
end

Private Instance Methods

object_data_add(oDataObject) click to toggle source

Add function. Add a Lorj::Data (data or list) to the ObjectData list.

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

  • Args :

  • Returns : Nothing

  • Raises : nothing

# File lib/core/core_object_data.rb, line 306
def object_data_add(oDataObject)
  object_type = oDataObject.object_type?

  if oDataObject.type == :list
    old_data_object = @params.rh_get(:query, object_type)
    old_data_object.unregister if old_data_object
    @params.rh_set(oDataObject, :query, object_type)
  else
    old_data_object = @params.rh_get(object_type)
    old_data_object.unregister if old_data_object
    @params[object_type] = oDataObject
  end
end
object_data_delete(obj) click to toggle source

delete function. delete a Lorj::Data (data or list) from the ObjectData list.

  • Args :

  • Returns : Nothing

  • Raises : nothing

# File lib/core/core_object_data.rb, line 329
def object_data_delete(obj)
  PrcLib.runtime_fail 'ObjectData: delete error. obj is not a'\
                       " framework data Object. Is a '%s'",
                      obj.class unless obj.is_a?(Lorj::Data)
  if obj.type == :list
    @params.rh_del(:query, obj.object_type?)
  else
    object_type = obj.object_type?
    @params.delete(object_type)
  end
end
object_data_exist?(object, key) click to toggle source
# File lib/core/core_object_data.rb, line 341
def object_data_exist?(object, key)
  # Return true if ObjectData Element is found when asked.
  return true if key[1] == :ObjectData && object.type?(key[0]) == :object

  # Return true if attritutes or controller object attributes found when
  # asked.
  return object.exist?(key[2..-1]) if key[1] == :attrs
  return object.exist?(key[1..-1]) if key.length > 1
  true
end
object_data_get(object, *key) click to toggle source

Get function

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

  • Args :

    • object: Lorj::Data object to get data. Must exist.

    • key : key tree (list of keys) If key == :attrs, get will forcelly use the Lorj::Data object attributes If key == :ObjectData, get will forcelly return the controller object otherwise, get will depends on the context:

      • controller context: will return the controller object

      • Process context: will return the Lorj::Data object attributes

  • Returns : value found or nil.

  • Raises : nothing

# File lib/core/core_object_data.rb, line 274
def object_data_get(object, *key)
  key = key.flatten

  return nil unless object.is_a?(Lorj::Data)

  # Return ObjectData Element if asked. Ignore additional keys.
  return @params[key[0]] if key[1] == :ObjectData

  # Return attributes if asked
  return object[:attrs,  key[2..-1]] if key[1] == :attrs

  # params are retrieved in process context
  # By default, if key is detected as a framework object, return its
  # data.
  return object[:attrs,  key[1..-1]] if @internal

  # params are retrieved in controller context
  # By default, if key is detected as a controller object, return its
  # data.
  return object[:object,  key[1..-1]] unless @internal
end