Class: DynamicImageElements::TableElement

Inherits:
Object
  • Object
show all
Includes:
ElementInterface
Defined in:
lib/elements/table_element.rb

Overview

Element is used for creating a table structure. It is composition of cells and rows of table.

Instance Method Summary collapse

Methods included from ElementInterface

#final_size, #set_height, #set_width

Constructor Details

#initialize(options, parent = nil, &block) ⇒ TableElement

Table element accepts options Hash. Block can be given and class provides element self to composite cells in it.

You can create table structure by two ways. By basic hierarchy where cells are nested in row block or set columns number by :cols option and use cells only. You can also combine these two ways by using row method to wrap current row before reaching its end.

Example

Using basic hierarchy:

table do
  row do
    cell do
      text "cell 1 in row 1"
    end
    cell do
      text "cell 2 in row 1"
    end
  end
  row do
    cell do
      text "cell 1 in row 2"
    end
    cell do
      text "cell 2 in row 2"
    end
  end
end

Using :cols option:

table :cols => 2 do
  cell do
    text "cell 1 in row 1"
  end
  cell do
    text "cell 2 in row 1"
  end

  cell do
    text "cell 1 in row 2"
  end
  cell do
    text "cell 2 in row 2"
  end
end

Options

You can use also aliases provided by ElementInterface::OPTIONS_ALIASES in all options Hashes.

:background

Described in BlockElement.

:border

Described in BlockElement.

:cols

Sets number of columns in table structure. If value is given it will automatically sort cells to rows.

You can also manually wrap the row by calling row method between cells.



63
64
65
66
67
68
69
70
71
72
# File 'lib/elements/table_element.rb', line 63

def initialize(options, parent = nil, &block) # :yields: table_element
  @options = options
  @parent = parent
  @elements = []
  @cells_map = [[]]
  @map_pos = [0, 0]
  use_options :margin
  @cols = options[:cols] ? options[:cols].to_i : nil
  process self, &block if block
end

Instance Method Details

#cell(options = {}, &block) ⇒ Object

Creates new TableCellElement as a cell of tables composite. See TableCellElement.new for arguments information.



199
200
201
202
203
204
205
# File 'lib/elements/table_element.rb', line 199

def cell(options = {}, &block) # :yields: cell_element
  zero_percent = {}
  zero_percent[:width] = "0%" if options[:width] == "0%"
  zero_percent[:height] = "0%" if options[:height] == "0%"
  treat_options options
  add_element TableCellElement.new(options, self, &block), options.merge(zero_percent)
end

#row(&block) ⇒ Object

Creates row of cells and gives block for compositing inner elements in table.

You can also call it alone to make next cells in new row.



210
211
212
213
214
215
216
217
218
# File 'lib/elements/table_element.rb', line 210

def row(&block) # :yields: table_element
  process self, &block if block
  if @map_pos[0] > 0
    @map_pos = [0, @map_pos[1] + 1]
    @cells_map[@map_pos[1]] ||= []
    move_to_next_pos
  end
  self
end