class Egalite::TableHelper

Public Class Methods

table_by_array(header, content, table_opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 84
def self.table_by_array(header, content, table_opts = {})
  _table(header, content, table_opts) { |line|
    line.map { |s| "<td>#{escape_html(s)}</td>" }.join
  }
end
table_by_hash(keys, header, content, table_opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 75
def self.table_by_hash(keys, header, content, table_opts = {})
  unless keys.size == header.size
    raise ArgumentError, "key and header count mismatch"
  end

  _table(header, content, table_opts) { |line|
    keys.map { |key| "<td>#{escape_html(line[key])}</td>"}.join
  }
end

Private Class Methods

_table(header, content, table_opts) click to toggle source
# File lib/egalite/helper.rb, line 65
def self._table(header, content, table_opts)
  head = ""
  if header
    head = header.map {|s| "<th>#{escape_html(s)}</th>" }.join
    head = "  <tr>#{head}</tr>\n"
  end
  body = content.map { |line| "  <tr>#{yield(line)}</tr>\n" }.join
  NonEscapeString.new("<table#{opt(table_opts)}>\n#{head}#{body}</table>")
end
escape_html(s) click to toggle source
# File lib/egalite/helper.rb, line 62
def self.escape_html(s)
  s.is_a?(NonEscapeString) ? s : Rack::Utils.escape_html(s)
end
opt(opts) click to toggle source
# File lib/egalite/helper.rb, line 59
def self.opt(opts)
  opts.map { |k,v| " #{escape_html(k)}='#{escape_html(v)}'" }.join
end