Class: Lorj::ObjectData

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

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

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

def initialize(internal = false)
  @params = {}
  @params[:hdata] = {} unless internal
  @internal = internal
end

Instance Method Details

#<<(hHash) ⇒ Object

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



173
174
175
# File 'lib/core/core_object_data.rb', line 173

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/core/core_object_data.rb', line 100

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



119
120
121
122
# File 'lib/core/core_object_data.rb', line 119

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



134
135
136
137
138
139
140
141
142
# File 'lib/core/core_object_data.rb', line 134

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



153
154
155
156
157
158
159
160
161
162
# File 'lib/core/core_object_data.rb', line 153

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 different

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

  • Raises : PrcError

Returns:

  • (Boolean)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/core/core_object_data.rb', line 187

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

#to_sObject



225
226
227
228
229
230
# File 'lib/core/core_object_data.rb', line 225

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

Returns:

  • (Boolean)


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

def type?(key)
  return nil unless @params.rh_exist?(key)
  :data
  :DataObject if @params[key].type == :object
end