Class: DocxReport::Table

Inherits:
Object
  • Object
show all
Includes:
BlockValue
Defined in:
lib/docx_report/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BlockValue

#load_text_direction, #load_value

Constructor Details

#initialize(name, has_header = false, collection = nil, fields = [], tables = []) ⇒ Table

Returns a new instance of Table.



8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#has_headerObject (readonly)

Returns the value of attribute has_header.



6
7
8
# File 'lib/docx_report/table.rb', line 6

def has_header
  @has_header
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/docx_report/table.rb', line 6

def name
  @name
end

#recordsObject (readonly)

Returns the value of attribute records.



6
7
8
# File 'lib/docx_report/table.rb', line 6

def records
  @records
end

Instance Method Details

#add_field(name, value = nil, type = :text, &block) ⇒ Object



24
25
26
27
28
29
30
# 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) ⇒ Object



32
33
34
35
36
37
# 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) ⇒ Object



45
46
47
48
49
50
51
# 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) ⇒ Object



39
40
41
42
43
# 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_recordObject



18
19
20
21
22
# File 'lib/docx_report/table.rb', line 18

def new_record
  new_record = Record.new
  @records << new_record
  new_record
end