Module: Duxml::Spreadsheet

Included in:
Grammar
Defined in:
lib/duxml/meta/grammar/spreadsheet.rb

Overview

contains helper methods to convert spreadsheet of DTD rules into Duxml::Rules

Class Method Summary collapse

Class Method Details

.sheet_to_xml(path) ⇒ Object

Parameters:

  • path (String)

    spreadsheet file



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/duxml/meta/grammar/spreadsheet.rb', line 10

def self.sheet_to_xml(path)
  worksheet = RubyXL::Parser.parse(path)[0]
  attr_val_rule_hash = {}
  g = GrammarClass.new
  worksheet.each_with_index do |row, index|
    next if index == 0
    break if row[3].nil? || row[4].nil?
    element_name = row[3].value
    statement_str = row[4].value
    g << ChildrenRuleClass.new(element_name, statement_str)
    attribute_rules = row[5].value.split(/\n/)
    attribute_rules.each_with_index do |rule, i|
      next if i == 0 or rule.empty?
      attr_name, value_expr, attr_req = *rule.split
      next if [attr_name, value_expr, attr_req].any? do |s| s.empty? or s.match(/\w/).nil? end
      g << AttrsRuleClass.new(element_name, attr_name, attr_req)
      unless attr_val_rule_hash[attr_name]
        g << ValueRuleClass.new(attr_name, value_expr)
        attr_val_rule_hash[attr_name] = true
      end
    end # attribute_rules.each_with_index
  end # worksheet.each_with_index
  g
end