Class: ODFReport::Table

Inherits:
Object
  • Object
show all
Includes:
HashGsub, Nested
Defined in:
lib/odf-report/table.rb

Constant Summary

Constants included from HashGsub

HashGsub::HTML_ESCAPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashGsub

#hash_gsub!, #html_escape, #node_hash_gsub!, #odf_linebreak

Methods included from Nested

#get_collection_from_item, #get_fields_with_values, #replace_values!

Constructor Details

#initialize(opts) ⇒ Table

Returns a new instance of Table.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/odf-report/table.rb', line 8

def initialize(opts)
  @name             = opts[:name]
  @collection_field = opts[:collection_field]
  @collection       = opts[:collection]
  @parent           = opts[:parent]

  @fields = {}

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

Instance Attribute Details

#collection_fieldObject

Returns the value of attribute collection_field.



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

def collection_field
  @collection_field
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#fieldsObject

Returns the value of attribute fields.



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

def fields
  @fields
end

#headerObject

Returns the value of attribute header.



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

def header
  @header
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#rowsObject

Returns the value of attribute rows.



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

def rows
  @rows
end

Instance Method Details

#add_column(name, field = nil, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/odf-report/table.rb', line 20

def add_column(name, field=nil, &block)
  if field
    @fields[name] = lambda { |item| item.send(field)}
  elsif block_given?
    @fields[name] = block
  else
    @fields[name] = lambda { |item| item.send(name)}
  end
end

#populate!(row) ⇒ Object



30
31
32
# File 'lib/odf-report/table.rb', line 30

def populate!(row)
  @collection = get_collection_from_item(row, @collection_field) if row
end

#replace!(doc, row = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/odf-report/table.rb', line 34

def replace!(doc, row = nil)

  return unless table = find_table_node(doc)

  populate!(row)

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

  @collection.each do |data_item|

    new_node = get_next_row

    replace_values!(new_node, data_item)

    table.add_child(new_node)

  end

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

end