class DocxReport::Table

Attributes

has_header[R]
name[R]
records[R]

Public Class Methods

new(name, has_header = false, collection = nil, fields = [], tables = []) click to toggle source
# File lib/docx_report/table.rb, line 8
def initialize(name, has_header = false, collection = nil, fields = [],
               tables = [])
  @name = name
  @has_header = has_header
  @value = collection
  @records = []
  @fields = fields
  @tables = tables
end

Public Instance Methods

add_field(name, value = nil, type = :text, &block) click to toggle source
# File lib/docx_report/table.rb, line 24
def add_field(name, value = nil, type = :text, &block)
  field = Field.new(name, value || block, type)
  raise 'duplicate field name' if @fields.any? do |f|
    f.name == field.name
  end
  @fields << field
end
add_table(name, collection = nil, has_header = false, &block) click to toggle source
# File lib/docx_report/table.rb, line 32
def add_table(name, collection = nil, has_header = false, &block)
  raise 'duplicate table name' if @tables.any? { |t| t.name == name }
  table = Table.new name, has_header, collection || block
  @tables << table
  table
end
load_records(collection) click to toggle source
# File lib/docx_report/table.rb, line 45
def load_records(collection)
  collection.each do |item|
    record = new_record
    @tables.each { |table| record.tables << table.load_table(item) }
    @fields.each { |field| record.fields << field.load_field(item) }
  end
end
load_table(item) click to toggle source
# File lib/docx_report/table.rb, line 39
def load_table(item)
  table = Table.new(name, has_header, nil, @fields, @tables)
  table.load_records(load_value(item))
  table
end
new_record() click to toggle source
# File lib/docx_report/table.rb, line 18
def new_record
  new_record = Record.new
  @records << new_record
  new_record
end