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:
-
Process context: create/query/update/delete/get handler uses it to build the handler parameters and is passed to each process handler as 2nd parameter. ex: If a connection object is created as follow:
define_obj(:connection, :create_e => :connection_create) obj_needs(:data, :uri) then at runtime: lorj_object.create(:connection, :uri => 'http://example.org') will call 'connection_create' (params) def connection_creat(object_type, params) where object_type is ':connection' and params is a 'Lorj::ObjectData' containing :uri value.
The object behavior is adapted to the process usage. By default for Lorj::Data(:object), params will get or set object attributes. ex: params # => 'example.org'
params[:test] # => nil
-
Controller context: create/query/update/delete/get handler uses it to build controller parameters like hdata. The object behavior is adapted to the controller usage By default for Lorj::Data(:object), hParams will get or set controller object
-
Internally by
BaseDefinition
. to get aLorj::Data
cache.
Public Class Methods
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
Merge 2 ObjectData
.
-
Args :
-
hHash
:Hash
ofLorj::Data
. But it is possible to have differentobject 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
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 theLorj::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 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 :
-
oDataObject
:Lorj::Data
object
-
-
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 function. delete a Lorj::Data
(data or list) from the ObjectData
cache.
-
Args :
-
object
:Lorj::Data
or Symbol representing aLorj::Data
cached.
-
-
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
check Lorj::Data
attributes or object exists. Or check key/value pair existence.
-
Args :
-
hHash
:Hash
ofLorj::Data
. But it is possible to have differentobject 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
# 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 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
# 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
Determine the type of object identified by a key. Lorj::Data
attributes or object exists. Or check key/value pair existence.
-
Args :
-
key
: Key to check inObjectData
list.
-
-
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
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 :
-
oDataObject
:Lorj::Data
object
-
-
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
delete function. delete a Lorj::Data
(data or list) from the ObjectData
list.
-
Args :
-
oDataObject
:Lorj::Data
object
-
-
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
# 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
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 theLorj::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