Class: Lorj::Data

Inherits:
Object show all
Includes:
Lorj::DataRubySpec::Public
Defined in:
lib/core/lorj_data.rb,
lib/core/lorj_data.rb

Overview

This class is the Data object used by lorj object! This is a key component of lorj

You find this object in different places.

type:

  • object/data : The Data object contains any kind of data. This data contains 2 elements:

    • controler object : This is a ruby object managed by the controller. Only the controller has the knowledge to manage this kind of data.

    • attributes : This is the internal data mapping from the controller object. The controller helped to build this data thanks to the BaseController. get_attr / BaseController.set_attr Attributes are declared by the Data model in BaseDefinition. At least usually, each object has 2 attributes: :id and :name

  • list : The Data object contains a list of Lorj::Data

If the object is of type :list, following functions are usable:

  • length : return numbers of Lorj::Data

  • each / each_index : loop on the list, key/value or key_index. yield

    can return 'remove' to remove the element from the
    list during loop.
    

If the Data object is of type :data or :object or even :list, following functions are usable.

  • set/[]=/get//exist? : Basic get/set/exist? feature.

  • type?/object_type? : Determine the type of this object.

    ie :data (stands for :object as well) or :list
    
  • to_a : Array of Data attributes

  • empty? nil? : Identify if the object is empty. Avoid to use

    nil?.
    
  • register/unregister : Used by Lorj::BaseDefinition internal @object_data registered? : Determine if this object is stored in the global

    object cache.
    

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Lorj::DataRubySpec::Public

#[]=

Constructor Details

#initialize(type = :object) ⇒ Data

Initialize Lorj::Data object

  • Args :

    • type : default is :object Support :data/:object for single object data

      :list for a list of object data
      
  • Returns : hash - internal data object.

  • Raises : No exceptions



78
79
80
81
82
83
84
85
86
87
# File 'lib/core/lorj_data.rb', line 78

def initialize(type = :object)
  type = :data unless [:list, :object, :data].include?(type)
  @type = type
  case type
  when :data, :object
    @data = new_object
  when :list
    @data = new_object_list
  end
end

Instance Attribute Details

#is_registeredObject

A Lorj::Data can be cached by Lorj::ObjectData. When adding Lorj::Data to Lorj::ObjectData, Lorj::Data object will be registered. This function will determine if this object is registered or not.



465
466
467
# File 'lib/core/lorj_data.rb', line 465

def is_registered
  @is_registered
end

#typeObject

Return Lorj::Data object type



90
91
92
# File 'lib/core/lorj_data.rb', line 90

def type
  @type
end

Instance Method Details

#[](*key) ⇒ Object

Get value from Lorj::data

  • Args :

    • keys: See get function.

  • Returns :

    • self : Lorj::Data

  • Raises : No exceptions



238
239
240
# File 'lib/core/lorj_data.rb', line 238

def [](*key)
  get(*key)
end

#base=(base) ⇒ Object



92
93
94
95
# File 'lib/core/lorj_data.rb', line 92

def base=(base)
  @base = base if @base.nil? && base.is_a?(Lorj::BaseDefinition)
  @base
end

#each(sData = :list) ⇒ Object

yield loop on a list

  • Args :

    • yield : sAction = yield (elem), where action can :removed to

      remove the element from the list.
      
  • Returns : no values

  • Raises : No exceptions



418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/core/lorj_data.rb', line 418

def each(sData = :list)
  to_remove = []
  return nil if @type != :list || ![:object, :list].include?(sData)

  @data[:list].each do |elem|
    case yield (elem)
    when :remove
      to_remove << elem
    end
  end

  return if to_remove.length <= 0
  to_remove.each { |elem| @data[:list].delete(elem) }
end

#each_index(sData = :list) ⇒ Object

yield loop on a list

  • Args :

    • yield : sAction = yield (index), where action can :removed to

      remove the element from the list.
      
  • Returns : no values

  • Raises : No exceptions



445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/core/lorj_data.rb', line 445

def each_index(sData = :list)
  to_remove = []
  return nil if @type != :list || ![:object, :list].include?(sData)

  @data[:list].each_index do |iIndex|
    case yield (iIndex)
    when :remove
      to_remove << @data[:list][iIndex]
    end
  end
  return if to_remove.length <= 0

  to_remove.each { |elem| @data[:list].delete(elem) }
end

#empty?Boolean

return true if the Lorj::Data object is nil.

  • Args : No parameters

  • Returns :

    • true/false

  • Raises : No exceptions



378
379
380
# File 'lib/core/lorj_data.rb', line 378

def empty?
  @data[:object].nil?
end

#exist?(*key) ⇒ Boolean

return true if a data object exist or if an extracted attribute exist.

  • Args :

    • keys : Keys to verify.

  • Returns :

    • exist? : true or false.

  • Raises : No exceptions

Examples:

data = Lorj::Data.new()

puts data.exist?(:object)          # => false

data.set({ :name => 'toto'}, :object) { |oObject |
   {:real_name => oObject[:name]}
}
list = Lorj::Data.new()
list.base = self

puts data.exist?(:object)          # => false

list.set([{ :name => 'toto'}, {:name => 'test'}], :list) { |oObject |
   {:real_name => oObject[:name]}
}

 puts data.exist?(:object)         # => true
 puts data.exist?(:name)           # => true
 puts data.exist?(:test)           # => false
 puts data.exist?(:attrs, :name)   # => true
 puts list.exist?(0)               # => true
 puts list.exist?(0, :object)      # => true
 puts list.exist?(2)               # => false
 puts list.exist?(2, :object)      # => false
 puts list.exist?(0, :name)        # => true
 puts list.exist?(0, :test)        # => false


358
359
360
361
362
363
364
365
# File 'lib/core/lorj_data.rb', line 358

def exist?(*key)
  case @type
  when :data, :object
    elem_exist?(*key)
  when :list
    list_exist?(*key)
  end
end

#get(*key) ⇒ Object

Get value from Lorj::data Depending on Lorj::Data type, you can get:

  • :object

    • get internal object data (:object) ex: object = data

    • get attribute data ex:

      data = { :name => 'toto'}
      copy = Lorj::Data.new()
      copy.base = self
      copy.set(data, :object) { |oObject |
         {:real_name => oObject[:name]}
      }
      
      puts copy[:name]      # => nil
      puts copy[:real_name] # => 'toto'
      puts copy[:object]    # => { :name => 'toto'}
      puts copy[:attrs]     # => { :real_name => 'toto'}
      
  • :list

    • get internal object data (:object) ex: object = data

    • get stored query object data (:query) ex: object = data

    • get one element attribute or object data ex:

      data = [{ :name => 'toto'}, {:name => 'test'}]
      copy = Lorj::Data.new()
      copy.base = self
      copy.set(data, :list, { :name => /^t/ }) { |oObject |
         {:real_name => oObject[:name]}
      }
      
      puts copy[0]             # => { :real_name => 'toto'}
      puts copy[0][:object]    # => { :name => 'toto'}
      puts copy[0][:attrs]     # => { :real_name => 'toto'}
      puts copy[1]             # => { :real_name => 'test'}
      puts copy[1, :real_name] # => 'test'
      puts copy[1, :test]      # => nil
      
  • Args :

    • keys: See get function.

  • Returns :

    • self : Lorj::Data

  • Raises : No exceptions



290
291
292
293
294
295
296
297
298
299
# File 'lib/core/lorj_data.rb', line 290

def get(*key)
  return @data if key.length == 0

  case @type
  when :data, :object # Return only attrs or the real object.
    elem_get(*key)
  when :list
    list_get(*key)
  end
end

#lengthObject

return 0, 1 or N if the Lorj::Data object is nil. 0 if no objects stored 1 if an object exist even if type :object or :list >1 if a list is stored. It will give the number of elements in the list.

  • Args : No parameters

  • Returns :

    • >=0 : Number of elements

  • Raises : No exceptions



396
397
398
399
400
401
402
403
404
# File 'lib/core/lorj_data.rb', line 396

def length
  case @type
  when :data
    return 0 if self.empty?
    1
  when :list
    @data[:list].length
  end
end

#object_type?Boolean

Return :object type of the Lorj::Data object.

  • Args : Nothing

  • Returns :

    • type : Symbol or nil

      nil if no object.
      :object type
      
  • Raises : No exceptions



130
131
132
# File 'lib/core/lorj_data.rb', line 130

def object_type?
  @data[:object_type]
end

#refreshObject

Do an object refresh by calling the process_refresh function if the process has been provided.

For details on refresh call, see #BaseDefinition.process_refresh

  • Args : Nothing

  • Returns :

    • status : Boolean.

      true if refresh is successful
      false otherwise
      


109
110
111
112
113
114
115
# File 'lib/core/lorj_data.rb', line 109

def refresh
  return false if empty? || @base.nil?
  return false unless @base.is_a?(Lorj::BaseDefinition) &&
                      @base.class.method_defined?(:process_refresh)

  @base.process_refresh(self)
end

#registerObject

A Lorj::Data can be cached by Lorj::ObjectData. When adding Lorj::Data to Lorj::ObjectData, Lorj::Data object will be registered. Lorj::ObjectData will call this function to marked it as registered.

  • Args :

    none
    
  • Returns :

    • self

  • Raises : No exceptions



481
482
483
484
# File 'lib/core/lorj_data.rb', line 481

def register
  @is_registered = true
  self
end

#set(oObj, sObjType = nil, hQuery = {}) ⇒ Object

Set a Lorj::Data object and return itself.

There 3 usages:

  • Set from a Lorj::Data. ex: if data is already a Lorj::Data,

    copy = Lorj::Data.new()
    copy.base = self
    copy.set(data)
    
  • Set from an object, not Lorj::Data and not a list. ex:

    data = { :test => 'toto'}
    copy = Lorj::Data.new()
    copy.base = self
    copy.set(data, :object) { |oObject | oObject }
    
  • Set from a list of objects, not Lorj::Data and not a :object. ex:

    data = [{ :name => 'toto'}, {:name => 'test'}]
    copy = Lorj::Data.new()
    copy.base = self
    copy.set(data, :list, { :name => /^t/ }) { |oObject | oObject }
    
  • Args :

    • data : Lorj::Data or any other data.

    • +ObjType: required only if data is not a Lorj::Data Use :object to store and extract attributes Use :list to extract elements, store them and extract attributes for

      each of them. data must support each(oObject) loop function
      
    • Query : Optional. To store the query object used to get the list

      objects. It assumes ObjectType = :list
      
    • yield : code to extract data attributes from the

      object (:object or :list). Should return an hash containing
      attributes data.
      
  • Returns :

    • self : Lorj::Data

  • Raises : No exceptions



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/core/lorj_data.rb', line 173

def set(oObj, sObjType = nil, hQuery = {})
  return obj_data_set(oObj, sObjType) if oObj.is_a?(Lorj::Data)

  return nil unless block_given?

  # while saving the object, a mapping work is done?
  case @type
  when :data, :object
    @data[:object_type] = sObjType
    @data[:object] = oObj
    @data[:attrs] = yield(sObjType, oObj)
  when :list
    list_set(oObj, sObjType, hQuery) do |object_type, object|
      yield(object_type, object)
    end
  end
  self
end

#to_aObject

Get the list of elements in an array from Lorj::Data :list type.

  • Args : No parameters

  • Returns :

    • Elements : Array of elements

  • Raises : No exceptions



312
313
314
315
316
317
318
# File 'lib/core/lorj_data.rb', line 312

def to_a
  result = []
  each do |elem|
    result << elem[:attrs]
  end
  result
end

#to_sObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/core/lorj_data.rb', line 192

def to_s
  str = format("-- Lorj::Data --\nType: %s\nContent:\n", @type)
  str += format('%s <= ', @data[:object_type])
  str += format("(%s) :\n", @data[:object].class)
  if @type != :list
    str += @data[:attrs].to_yaml
    return str
  end
  str += format("query:\n%s", @data[:query].to_yaml)
  str += format("\nlist count: %s\n", @data[:list].length)
  elem_print = []
  @data[:list].each do |elem|
    elem_print << elem.to_s
  end
  str += elem_print.to_yaml
  str
end

#unregisterObject

A Lorj::Data can be cached by Lorj::ObjectData. When adding Lorj::Data to Lorj::ObjectData, Lorj::Data object will be registered. Lorj::ObjectData will call this function to marked it as unregistered.

  • Args :

    none
    
  • Returns :

    • self

  • Raises : No exceptions



500
501
502
503
# File 'lib/core/lorj_data.rb', line 500

def unregister
  @is_registered = false
  self
end