Class: Lorj::ObjectData

Inherits:
Object show all
Includes:
Lorj::ObjectDataRubySpec::Public
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

Methods included from Lorj::ObjectDataRubySpec::Public

#[]=

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



85
86
87
88
89
90
# File 'lib/core/core_object_data.rb', line 85

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 different

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

  • Raises : nothing



190
191
192
# File 'lib/core/core_object_data.rb', line 190

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ 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



151
152
153
154
155
156
157
158
159
# 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) ⇒ 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



170
171
172
173
174
175
176
177
178
179
# 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) ⇒ 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)


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# 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

#refreshObject



103
104
105
106
107
# 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) ⇒ Object

Refresh setting

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



97
98
99
100
101
# 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_sObject



245
246
247
248
249
250
# 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) ⇒ Boolean Also known as: otype?

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)


236
237
238
239
240
241
# 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