class RenderAsMarkdown::Table

Attributes

columns[RW]
rows[RW]

Public Class Methods

new(column_titles) click to toggle source
# File lib/render-as-markdown/table.rb, line 6
def initialize column_titles
  # column_titles will be an array
  @columns = [*column_titles].map{|title| Column.new title}
  @rows = []
end

Public Instance Methods

<<(row)
Alias for: add_row
add_row(row) click to toggle source
# File lib/render-as-markdown/table.rb, line 12
def add_row row
  # TODO: ensure element count of row is == element count of columns

  # make row an array
  row = [*row]

  # add row to rows, use an array
  @rows << row

  # iterate through columns and row, add each row to their column
  @columns.zip(row).each {|col, val| col.add_row val.to_s}
end
Also aliased as: <<
render() click to toggle source
# File lib/render-as-markdown/table.rb, line 28
def render
  render_head << render_seperator << render_body
end
Also aliased as: to_s
render_body() click to toggle source
# File lib/render-as-markdown/table.rb, line 44
def render_body
  # join all columns for all rows
  body =''
  @rows.each_with_index do |row, i|
    body << render_row( i )
  end
  body
end
render_head() click to toggle source
# File lib/render-as-markdown/table.rb, line 34
def render_head
  # join all column headers
  @columns.map(&:render_title).join( '|' ) << "\n"
end
render_row(row) click to toggle source
# File lib/render-as-markdown/table.rb, line 53
def render_row row
  @columns.map {|col| col.render_row row}.join( '|' ) << "\n"
end
render_seperator() click to toggle source
# File lib/render-as-markdown/table.rb, line 39
def render_seperator
  # join all column lines
  @columns.map(&:render_line).join( '|' ) << "\n"
end
to_s()
Alias for: render