Class: Lorj::ObjectData
Overview
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 a Lorj::Data cache.
Instance Method Summary collapse
-
#<<(hHash) ⇒ Object
Merge 2 ObjectData.
-
#[](*key) ⇒ Object
Get function.
-
#[]=(*key, value) ⇒ Object
Functions used to set simple data/Object for controller/process function call.
-
#add(oDataObject) ⇒ Object
Add function.
-
#delete(obj) ⇒ Object
delete function.
-
#exist?(*key) ⇒ Boolean
check Lorj::Data attributes or object exists.
-
#initialize(internal = false) ⇒ ObjectData
constructor
Initialize the object.
- #refresh ⇒ Object
-
#refresh_set(base_def_instance, object_type, sEventType, as_controller) ⇒ Object
Refresh setting.
- #to_s ⇒ Object
-
#type?(key) ⇒ Boolean
Determine the type of object identified by a key.
Constructor Details
#initialize(internal = false) ⇒ ObjectData
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
77 78 79 80 81 82 |
# File 'lib/core/core_object_data.rb', line 77 def initialize(internal = false) @params = {} @params[:hdata] = {} unless internal @internal = internal @refresh = nil end |
Instance Method Details
#<<(hHash) ⇒ Object
Merge 2 ObjectData.
-
Args :
-
hHash: Hash of Lorj::Data. But it is possible to have differentobject type (not Lorj::Data)
-
-
Returns : hash merged
-
Raises : nothing
191 192 193 |
# File 'lib/core/core_object_data.rb', line 191 def <<(hHash) @params.merge!(hHash) unless hHash.nil? end |
#[](*key) ⇒ Object
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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/core/core_object_data.rb', line 118 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 |
#[]=(*key, value) ⇒ Object
Functions used to set simple data/Object for controller/process function call. TODO: to revisit this function, as we may consider simple data, as Lorj::Data object
137 138 139 140 |
# File 'lib/core/core_object_data.rb', line 137 def []=(*key, value) return nil if [:object, :query].include?(key[0]) @params.rh_set(value, key) end |
#add(oDataObject) ⇒ Object
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
152 153 154 155 156 157 158 159 160 |
# File 'lib/core/core_object_data.rb', line 152 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) ⇒ Object
delete function. delete a Lorj::Data (data or list) from the ObjectData cache.
-
Args :
-
object: Lorj::Data or Symbol representing a Lorj::Data cached.
-
-
Returns : Nothing
-
Raises : nothing
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/core/core_object_data.rb', line 171 def delete(obj) if obj.is_a?(Symbol) object_type = obj obj = @params[object_type] @params[object_type] = nil else object_data_delete(obj) end obj.unregister unless obj.nil? end |
#exist?(*key) ⇒ Boolean
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 differentobject type (not Lorj::Data)
-
-
Returns : true/false
-
Raises : PrcError
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/core/core_object_data.rb', line 205 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 ⇒ Object
95 96 97 98 99 |
# File 'lib/core/core_object_data.rb', line 95 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) ⇒ Object
Refresh setting
This function is used to provide capability to an Object to be refreshed from Lorj Cache.
89 90 91 92 93 |
# File 'lib/core/core_object_data.rb', line 89 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 ⇒ Object
243 244 245 246 247 248 |
# File 'lib/core/core_object_data.rb', line 243 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) ⇒ Boolean
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 in ObjectData 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
237 238 239 240 241 |
# File 'lib/core/core_object_data.rb', line 237 def type?(key) return nil unless @params.rh_exist?(key) :data :DataObject if @params[key].type == :object end |