Sha256: c5e2e53f2585a31340be3a9c70bfb516716ad201166918933980c131707d1ea4

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

require 'rexml/document'
require_relative 'row'

# This class process "template" tag used by Tables
class Template
  attr_reader :datarows

  def initialize(table, index, xml)
    @mode = :simple
    vars = load_vars_from(xml)
    template = load_template_from(xml)
    data_string = apply_vars_to_template(vars, template)
    @datarows = read_rows_from(table, index, data_string)
  end

  def load_vars_from(xml)
    vars = {}
    v = xml.attributes
    v.keys.each do |i|
      if i == 'mode'
        @mode = v[i].to_sym
      else
        vars[i] = v[i].split(',')
      end
    end
    # fill_vars_values(vars,mode)
    vars
  end

  def fill_vars_values(vars, mode)
    # create sizes array
  end

  def load_template_from(xml)
    template = ''
    xml.elements.each { |i| template << i.to_s + "\n" }
    template
  end

  def apply_vars_to_template(vars, template)
    output = ''
    return if vars.size.zero?
    max = vars.first[1].size
    (1..max).each do |index|
      t = template.dup
      vars.each_pair { |k, v| t.gsub!(k, v[index - 1]) }
      output += t
    end
    output
  end

  def read_rows_from(table, index, data_string)
    datarows = []
    data = "<template>\n#{data_string}\n</template>"
    xml = REXML::Document.new(data)
    xml.root.elements.each do |i|
      if i.name == 'row'
        datarows << Row.new(table, index, i)
        index += 1
      end
    end
    datarows
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
asker-tool-2.1.7 lib/asker/data/template.rb
asker-tool-2.1.6 lib/asker/data/template.rb
asker-tool-2.1.5 lib/asker/data/template.rb
asker-tool-2.1.3 lib/asker/data/template.rb
asker-tool-2.1.2 lib/asker/data/template.rb