class Table

Contains data table information

Attributes

datarows[R]
fields[R]
id[R]
langs[R]
name[R]
rows[R]
sequence[R]
simple[R]

Table is simple when all their rows and col has the same lang and type value

Public Class Methods

new(concept, xml_data) click to toggle source

initialize Table object @param concept (Concept) @param xml_data (XML)

# File lib/asker/data/table.rb, line 20
def initialize(concept, xml_data)
  @concept = concept
  read_attributes_from_xml(xml_data)

  @simple = { lang: true, type: true }
  @types  = ['text']        * @fields.size
  @langs  = [@concept.lang] * @fields.size

  @datarows = [] # DEV experiment replace row data with row objects
  read_data_from_xml(xml_data)
  @rows = @datarows.map(&:raws)
end

Public Instance Methods

sequence?() click to toggle source

Return true if table has a sequence defined

# File lib/asker/data/table.rb, line 41
def sequence?
  @sequence.size.positive?
end
simple_off(option) click to toggle source

Set table to simple off

@param option (Symbol)

# File lib/asker/data/table.rb, line 62
def simple_off(option)
  @simple[option] = false
end
to_s() click to toggle source

Return table name

# File lib/asker/data/table.rb, line 35
def to_s
  @name.to_s
end
types(index = :all) click to toggle source

Return fields type:

  • types(:all) => Return an Array with all field types

  • types(index) => Return type for fields

@param index (Integer)

# File lib/asker/data/table.rb, line 50
def types(index = :all)
  @types = (['text'] * @fields.size) if @types.nil?
  return @types if index == :all

  @types[index]
end

Private Instance Methods

read_attributes_from_xml(xml_data) click to toggle source

Fill:fields, name and id from XML input @param xml_data (XML) rubocop:disable Metrics/AbcSize

# File lib/asker/data/table.rb, line 72
def read_attributes_from_xml(xml_data)
  # read attributes from XML data
  t = xml_data.attributes['fields'].to_s.strip.split(',')
  t.each(&:strip!)
  @fields = t || []

  @name = ''
  @fields.each { |i| @name = "#{@name}$#{i.to_s.strip.downcase}" }
  @id = "#{@concept.name}.#{@name}"

  @sequence = []
  return unless xml_data.attributes['sequence']

  @sequence = xml_data.attributes['sequence'].to_s.split(',')
end
read_data_from_xml(xml_data) click to toggle source

Build table data from xml input @param xml_data (XML) rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/asker/data/table.rb, line 94
def read_data_from_xml(xml_data)
  xml_data.elements.each do |i|
    case i.name
    when 'lang'
      read_lang_from_xml(i)
    when 'row'
      @datarows << Row.new(self, @datarows.size, i)
    when 'sequence'
      @sequence = i.text.split(',')
    when 'template'
      @datarows += Template.new(self, @datarows.size, i).datarows
    when 'type'
      read_type_from_xml(i)
    else
      puts Rainbow("[ERROR] concept/table#xml_data with #{i.name}").red.bright
    end
  end
end
read_lang_from_xml(xml_data) click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/asker/data/table.rb, line 117
def read_lang_from_xml(xml_data)
  j = xml_data.text.split(',')
  codes = @langs.map(&:code)
  return if j.join(',') == codes.join(',')

  simple_off(:lang)
  @langs = []
  j.each do |k|
    if ['*', ''].include? k.strip
      @langs << @concept.lang
    else
      @langs << LangFactory.instance.get(k.strip.to_s)
    end
  end
end
read_type_from_xml(xml_data) click to toggle source

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize

# File lib/asker/data/table.rb, line 135
def read_type_from_xml(xml_data)
  j = xml_data.text.split(',')
  return if j.join(',') == @types.join(',')

  simple_off(:type)
  @types = []
  j.each { |k| @types << k.strip.to_s }
end