Class: ODFWriter::Table

Inherits:
Object
  • Object
show all
Includes:
Nested
Defined in:
lib/odf_writer/table.rb

Overview

Table: poulate and grow tables

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Nested

#add_bookmark, #add_field, #add_image, #add_section, #add_table, #add_text, #items, #populate

Constructor Details

#initialize(options) ⇒ Table

initialize



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/odf_writer/table.rb', line 40

def initialize(options)
  @name          = options[:name]
  @field         = options[:field]
  @collection    = options[:collection]
  @proc          = options[:proc]
  @key           = @field || @name
     
  @fields        = []
  @texts         = []
  @tables        = []
  @images        = []
  @bookmarks     = []
  
  @template_rows = []
  @header        = options[:header] || false
  @skip_if_empty = options[:skip_if_empty] || false
  
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



33
34
35
# File 'lib/odf_writer/table.rb', line 33

def collection
  @collection
end

#nameObject

Returns the value of attribute name.



33
34
35
# File 'lib/odf_writer/table.rb', line 33

def name
  @name
end

#procObject

Returns the value of attribute proc.



33
34
35
# File 'lib/odf_writer/table.rb', line 33

def proc
  @proc
end

Instance Method Details

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

replace!



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/odf_writer/table.rb', line 64

def replace!(doc, manifest, file, row = nil)

  return unless table = find_table_node(doc)
  
  @template_rows = table.xpath("table:table-row")
  
  @header = table.xpath("table:table-header-rows").empty? ? @header : false
  
  @collection = items(row, @key, @proc) if row
  
  if @skip_if_empty && @collection.empty?
    table.remove
    return
  end
  
  @collection.each do |item|
  
    new_node = get_next_row
    #
    # experimental: new node must be added to doc prior to replace!
    #               else new_section does not have a name space
    #
    table.add_child(new_node)
    
    @tables.each    { |t| t.replace!(new_node, manifest, file, item) }
    @texts.each     { |t| t.replace!(new_node, item) }
    @fields.each    { |f| f.replace!(new_node, item) }
    @images.each    { |f| f.replace!(new_node, manifest, file, item) }
    
  end
  Image.unique_image_names( doc) if @images.present?
  
  @template_rows.each_with_index do |r, i|
    r.remove if (get_start_node..template_length) === i
  end
  
end