Class: ODFReport::Table

Inherits:
Nestable show all
Defined in:
lib/odf-report/table.rb

Instance Method Summary collapse

Methods inherited from Nestable

#add_field, #add_image, #add_section, #add_table, #add_text, #all_images, #set_source, #wrap_with_ns

Constructor Details

#initialize(opts) ⇒ Table

Returns a new instance of Table.



4
5
6
7
8
9
10
# File 'lib/odf-report/table.rb', line 4

def initialize(opts)
  super(opts)

  @template_rows = []
  @header           = opts[:header] || false
  @skip_if_empty    = opts[:skip_if_empty] || false
end

Instance Method Details

#replace!(doc) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/odf-report/table.rb', line 12

def replace!(doc)
  return unless table = find_table_node(doc)

  @template_rows = table.xpath("table:table-row")

  @header = table.xpath("table:table-header-rows").empty? ? @header : false

  if @skip_if_empty && @data_source.empty?
    table.remove
    return
  end

  @data_source.each do |record|

    new_node = get_next_row

    @tables.each    { |n| n.set_source(record).replace!(new_node) }
    @sections.each  { |n| n.set_source(record).replace!(new_node) }
    @texts.each     { |n| n.set_source(record).replace!(new_node) }
    @fields.each    { |n| n.set_source(record).replace!(new_node) }
    @images.each    { |n| n.set_source(record).replace!(new_node) }

    table.add_child(new_node.to_xml)

  end

  @template_rows.each_with_index do |r, i|
    r.remove if (get_start_node..template_length) === i
  end

end