class PostRunner::FlexiTable

Attributes

column_attributes[R]
frame[R]

Public Class Methods

new(&block) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 160
def initialize(&block)
  @head_rows = []
  @body_rows = []
  @foot_rows = []
  @column_count = 0

  @current_section = :body
  @current_row = nil

  @frame = true
  @html_attrs = { :class => 'flexitable' }

  @column_attributes = []

  instance_eval(&block) if block_given?
end

Public Instance Methods

body() click to toggle source
# File lib/postrunner/FlexiTable.rb, line 185
def body
  @current_section = :body
end
cell(content, attributes = {}) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 202
def cell(content, attributes = {})
  if @current_row.nil?
    case @current_section
    when :head
      @head_rows
    when :body
      @body_rows
    when :foot
      @foot_rows
    else
      raise "Unknown section #{@current_section}"
    end << (@current_row = Row.new(self, @current_section))
  end
  @current_row.cell(content, attributes)
end
enable_frame(enabled) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 238
def enable_frame(enabled)
  @frame = enabled
end
foot() click to toggle source
# File lib/postrunner/FlexiTable.rb, line 189
def foot
  @current_section = :foot
end
head() click to toggle source
# File lib/postrunner/FlexiTable.rb, line 181
def head
  @current_section = :head
end
new_row() click to toggle source
# File lib/postrunner/FlexiTable.rb, line 193
def new_row
  if @current_row && @head_rows[0] &&
     @current_row.length != @head_rows[0].length
    Log.fatal "Row has #{@current_row.length} cells instead of " +
              "#{@head_rows[0].length} cells in head row."
  end
  @current_row = nil
end
row(cells, attributes = {}) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 218
def row(cells, attributes = {})
  cells.each { |c| cell(c) }
  set_row_attributes(attributes)
  new_row
end
set_column_attributes(col_attributes) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 224
def set_column_attributes(col_attributes)
  col_attributes.each.with_index do |ca, idx|
    @column_attributes[idx] = Attributes.new(ca)
  end
end
set_html_attrs(name, value) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 177
def set_html_attrs(name, value)
  @html_attrs[name] = value
end
set_row_attributes(row_attributes) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 230
def set_row_attributes(row_attributes)
  unless @current_row
    raise "No current row. Use after first cell definition but before " +
          "new_row call."
  end
  @current_row.set_row_attributes(row_attributes)
end
to_html(doc) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 257
def to_html(doc)
  index_table

  doc.unique(:flexitable_style) {
    doc.head { doc.style(style) }
  }
  doc.table(@html_attrs) {
    @head_rows.each { |r| r.to_html(doc) }
    @body_rows.each { |r| r.to_html(doc) }
    @foot_rows.each { |r| r.to_html(doc) }
  }
end
to_s() click to toggle source
# File lib/postrunner/FlexiTable.rb, line 242
def to_s
  index_table
  calc_terminal_columns

  s = frame_line_to_s
  s << rows_to_s(@head_rows)
  s << frame_line_to_s unless @head_rows.empty?
  s << rows_to_s(@body_rows)
  s << frame_line_to_s unless @body_rows.empty?
  s << rows_to_s(@foot_rows)
  s << frame_line_to_s unless @foot_rows.empty?

  s
end

Private Instance Methods

calc_section_teminal_columns(col_idx, col_mtw, rows) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 301
def calc_section_teminal_columns(col_idx, col_mtw, rows)
  rows.each do |r|
    if r[col_idx].nil?
      raise ArgumentError, "Not all rows have same number of cells"
    end

    mtw = r[col_idx].min_terminal_width
    if col_mtw.nil? || col_mtw < mtw
      col_mtw = mtw
    end
  end

  col_mtw
end
calc_terminal_columns() click to toggle source
# File lib/postrunner/FlexiTable.rb, line 288
def calc_terminal_columns
  @column_count.times do |i|
    col_mtw = nil

    col_mtw = calc_section_teminal_columns(i, col_mtw, @head_rows)
    col_mtw = calc_section_teminal_columns(i, col_mtw, @body_rows)
    col_mtw = calc_section_teminal_columns(i, col_mtw, @foot_rows)

    @column_attributes[i] = Attributes.new unless @column_attributes[i]
    @column_attributes[i].min_terminal_width = col_mtw
  end
end
frame_line_to_s() click to toggle source
# File lib/postrunner/FlexiTable.rb, line 320
def frame_line_to_s
  return '' unless @frame
  s = '+'
  @column_attributes.each do |c|
    s += '-' * c.min_terminal_width + '+'
  end
  s + "\n"
end
index_table() click to toggle source
# File lib/postrunner/FlexiTable.rb, line 272
def index_table
  @column_count = (@head_rows[0] || @body_rows[0]).length

  @column_count.times do |i|
    index_table_rows(i, @head_rows)
    index_table_rows(i, @body_rows)
    index_table_rows(i, @foot_rows)
  end
end
index_table_rows(col_idx, rows) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 282
def index_table_rows(col_idx, rows)
  rows.each.with_index do |r, row_idx|
    r.set_indicies(col_idx, row_idx)
  end
end
rows_to_s(x_rows) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 316
def rows_to_s(x_rows)
  x_rows.empty? ? '' : (x_rows.map { |r| r.to_s}.join("\n") + "\n")
end
style() click to toggle source
# File lib/postrunner/FlexiTable.rb, line 329
    def style
      <<EOT
.flexitable {
  width: 100%;
  border: 2px solid #545454;
  border-collapse: collapse;
  font-size:11pt;
}
.ft_head_row {
  background-color: #DEDEDE
}
.ft_even_row {
  background-color: #FCFCFC
}
.ft_odd_row {
  background-color: #F1F1F1
}
.ft_cell {
  border: 1px solid #CCCCCC;
  padding: 1px 3px;
}
EOT
    end