class Lorj::Data

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:

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

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

Attributes

is_registered[RW]

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.

type[R]

Return Lorj::Data object type

Public Class Methods

new(type = :object) click to toggle source

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

# 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

Public Instance Methods

[](*key) click to toggle source

Get value from Lorj::data

  • Args :

    • keys: See get function.

  • Returns :

  • Raises : No exceptions

# File lib/core/lorj_data.rb, line 238
def [](*key)
  get(*key)
end
base=(base) click to toggle source
# 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) { |elem| ... } click to toggle source

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

# 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) { |iIndex| ... } click to toggle source

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

# 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?() click to toggle source

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

  • Args : No parameters

  • Returns :

    • true/false

  • Raises : No exceptions

# File lib/core/lorj_data.rb, line 378
def empty?
  @data[:object].nil?
end
exist?(*key) click to toggle source

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
# 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) click to toggle source

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 :

  • Raises : No exceptions

# 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
length() click to toggle source

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

# 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?() click to toggle source

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

  • Args : Nothing

  • Returns :

    • type : Symbol or nil

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

# File lib/core/lorj_data.rb, line 130
def object_type?
  @data[:object_type]
end
refresh() click to toggle source

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
# 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
register() click to toggle source

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

# File lib/core/lorj_data.rb, line 481
def register
  @is_registered = true
  self
end
set(oObj, sObjType = nil, hQuery = {}) { |sObjType, oObj| ... } click to toggle source

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 :

  • Raises : No exceptions

# 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_a() click to toggle source

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

  • Args : No parameters

  • Returns :

    • Elements : Array of elements

  • Raises : No exceptions

# File lib/core/lorj_data.rb, line 312
def to_a
  result = []
  each do |elem|
    result << elem[:attrs]
  end
  result
end
to_s() click to toggle source
# 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
type=(sObjType) click to toggle source

Set the :object type

  • Args :

    • +ObjType: required only if data is not a Lorj::Data

  • Returns :

  • Raises : No exceptions

# File lib/core/lorj_data.rb, line 221
def type=(sObjType)
  return self if self.empty?
  @data[:object_type] = sObjType
  self
end
unregister() click to toggle source

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

# File lib/core/lorj_data.rb, line 500
def unregister
  @is_registered = false
  self
end

Private Instance Methods

elem_exist?(*key) click to toggle source
# File lib/core/lorj_data.rb, line 570
def elem_exist?(*key)
  return true if key[0] == :object && @data.key?(key[0])
  return true if key[0] == :attrs && @data.rh_exist?(key)
  (@data.rh_lexist?(:attrs, key) == key.length + 1)
end
elem_get(*key) click to toggle source
# File lib/core/lorj_data.rb, line 557
def elem_get(*key)
  return @data[key[0]] if key[0] == :object
  return @data.rh_get(key) if key[0] == :attrs
  @data.rh_get(:attrs, key)
end
list_exist?(*key) click to toggle source
# File lib/core/lorj_data.rb, line 576
def list_exist?(*key)
  return true if key[0] == :object && @data.key?(key[0])
  @data[:list][key[0]].rh_exist?(:attrs, key[1..-1])
end
list_get(*key) click to toggle source
# File lib/core/lorj_data.rb, line 563
def list_get(*key)
  return @data[key[0]] if [:object, :query].include?(key[0])
  return @data[:list][key[0]] if key.length == 1
  # can Return only attrs or the real object.
  @data[:list][key[0]][key[1..-1]]
end
list_set(oObj, sObjType = nil, hQuery = {}) { |sObjectType, oObject| ... } click to toggle source
# File lib/core/lorj_data.rb, line 512
def list_set(oObj, sObjType = nil, hQuery = {})
  @data[:object] = oObj
  @data[:object_type] = sObjType
  @data[:query] = hQuery

  return self if oObj.nil?

  begin
    oObj.each do |oElemObject|
      next if oElemObject.nil?
      begin
        data_obj = Lorj::Data.new(:object)
        data_obj.base = @base
        data_obj.set(oElemObject, sObjType) do |sObjectType, oObject|
          yield(sObjectType, oObject)
        end
        @data[:list] << data_obj
      rescue => e
        PrcLib.runtime_fail "'%s' Mapping attributes issue.\n%s",
                            sObjType, e.message
      end
    end
  rescue => e
    PrcLib.runtime_fail "each function is not supported by '%s'.\n%s",
                        oObj.class, e.message
  end
  self
end
new_object() click to toggle source

Define minimal @data structure for a :object object type.

# File lib/core/lorj_data.rb, line 592
def new_object
  { :object_type => nil,
    :attrs => {},
    :object => nil
  }
end
new_object_list() click to toggle source

Define minimal @data structure for a :list object type.

# File lib/core/lorj_data.rb, line 582
def new_object_list
  {
    :object => nil,
    :object_type => nil,
    :list => [],
    :query => nil
  }
end
obj_data_set(oObj, sObjType = nil) click to toggle source
# File lib/core/lorj_data.rb, line 541
def obj_data_set(oObj, sObjType = nil)
  type = oObj.type?
  case type
  when :data, :object
    @data[:object_type] = (sObjType.nil?) ? (oObj.object_type?) : sObjType
    @data[:object] = oObj.get(:object)
    @data[:attrs] = oObj.get(:attrs)
  when :list
    @data[:object_type] = (sObjType.nil?) ? (oObj.object_type?) : sObjType
    @data[:object] = oObj.get(:object)
    @data[:list] = oObj.get(:list)
    @data[:query] = oObj.get(:query)
  end
  self
end