Class: Template
- Inherits:
-
Object
- Object
- Template
- Defined in:
- lib/asker/data/template.rb
Overview
This class process “template” tag used by Tables
Instance Attribute Summary collapse
-
#datarows ⇒ Object
readonly
Returns the value of attribute datarows.
Instance Method Summary collapse
- #apply_vars_to_template(vars, template) ⇒ Object
- #fill_vars_values(vars, mode) ⇒ Object
-
#initialize(table, index, xml) ⇒ Template
constructor
A new instance of Template.
- #load_template_from(xml) ⇒ Object
- #load_vars_from(xml) ⇒ Object
- #read_rows_from(table, index, data_string) ⇒ Object
Constructor Details
#initialize(table, index, xml) ⇒ Template
Returns a new instance of Template.
10 11 12 13 14 15 16 |
# File 'lib/asker/data/template.rb', line 10 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 |
Instance Attribute Details
#datarows ⇒ Object (readonly)
Returns the value of attribute datarows.
8 9 10 |
# File 'lib/asker/data/template.rb', line 8 def datarows @datarows end |
Instance Method Details
#apply_vars_to_template(vars, template) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/asker/data/template.rb', line 42 def apply_vars_to_template(vars, template) output = "" return output 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 |
#fill_vars_values(vars, mode) ⇒ Object
32 33 34 |
# File 'lib/asker/data/template.rb', line 32 def fill_vars_values(vars, mode) # create sizes array end |
#load_template_from(xml) ⇒ Object
36 37 38 39 40 |
# File 'lib/asker/data/template.rb', line 36 def load_template_from(xml) template = "" xml.elements.each { |i| template << i.to_s + "\n" } template end |
#load_vars_from(xml) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/asker/data/template.rb', line 18 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 |
#read_rows_from(table, index, data_string) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/asker/data/template.rb', line 55 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 |