Class: ThousandIsland::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/thousand_island/table.rb

Overview

The Table class can be used to set the definition of your table, such as format and headings. You then inject the data from your Builder so that it can be rendered. You can declare a TableSettings class that you wish to use, otherwise the default will be used. To set a Table Setting class, add the following in your class body:

uses_style MyTableSettings

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pdf) ⇒ Table

Returns a new instance of Table.



28
29
30
31
# File 'lib/thousand_island/table.rb', line 28

def initialize(pdf)
  @pdf = pdf
  @table_options = {}
end

Class Attribute Details

.table_settings_klassObject



19
20
21
# File 'lib/thousand_island/table.rb', line 19

def table_settings_klass
  @table_settings_klass ||= TableSettings
end

Instance Attribute Details

#pdfObject (readonly)

Returns the value of attribute pdf.



13
14
15
# File 'lib/thousand_island/table.rb', line 13

def pdf
  @pdf
end

#table_optionsObject

Returns the value of attribute table_options.



14
15
16
# File 'lib/thousand_island/table.rb', line 14

def table_options
  @table_options
end

Class Method Details

.uses_settings(klass) ⇒ Object



23
24
25
# File 'lib/thousand_island/table.rb', line 23

def uses_settings(klass)
  self.table_settings_klass = klass
end

Instance Method Details

#body_rowsObject



56
57
58
# File 'lib/thousand_island/table.rb', line 56

def body_rows
  @body_rows ||= []
end

#body_rows=(row_array) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
# File 'lib/thousand_island/table.rb', line 51

def body_rows=(row_array)
  raise ArgumentError.new('table_rows must be an array') unless row_array.is_a?(Array)
  @body_rows = row_array
end

#draw(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/thousand_island/table.rb', line 37

def draw(options={})

  @table_options = merged_options(options)

  table_options[:header] = prawn_header_setting

  pdf.table(table_data, options_for_prawn) do |t|
    t.row(0..num_header_rows - 1).font_style = table_options[:header_format][:font_style] unless header_rows.empty?
    t.row(0..num_header_rows - 1).align = table_options[:header_format][:align] unless header_rows.empty?
    t.row(-1..(0 - num_footer_rows)).font_style = :bold unless footer_rows.empty?
    yield(t) if block_given?
  end
end


74
75
76
# File 'lib/thousand_island/table.rb', line 74

def footer_rows
  @footer_rows ||= []
end

Raises:

  • (ArgumentError)


69
70
71
72
# File 'lib/thousand_island/table.rb', line 69

def footer_rows=(row_array)
  raise ArgumentError.new('footer_rows must be an array') unless row_array.is_a?(Array)
  @footer_rows = row_array
end

#header_rowsObject



65
66
67
# File 'lib/thousand_island/table.rb', line 65

def header_rows
  @header_rows ||= []
end

#header_rows=(row_array) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
# File 'lib/thousand_island/table.rb', line 60

def header_rows=(row_array)
  raise ArgumentError.new('header_cells must be an array') unless row_array.is_a?(Array)
  @header_rows = row_array
end

#settingsObject



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

def settings
  {}
end