Module: DbAgile::IO::HTML

Defined in:
lib/dbagile/io/html.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ }

Class Method Summary collapse

Class Method Details

.to_html(data, columns = nil, buffer = "", options = {}) ⇒ ...

Outputs some data as an HTML string

Returns:

  • (...)

    the buffer itself



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dbagile/io/html.rb', line 20

def to_html(data, columns = nil, buffer = "", options = {})
  options = DEFAULT_OPTIONS.merge(options)
  with_tag("table", buffer, 0) {
    with_tag("thead", buffer, 1) {
      with_tag("tr", buffer, 2) {
        columns.each{|column|
          buffer << "  "*3 << "<td>#{CGI::escape(column.to_s)}</td>\n"
        }
      }
    }
    with_tag("tbody", buffer, 1) {
      data.each{|row|
        with_tag("tr", buffer, 2) {
          columns.each{|column|
            value = row[column]
            cssclazz = value.class.name.to_s.downcase
            buffer << "  "*3 << "<td class=\"#{cssclazz}\">#{CGI::escapeHTML(value.to_s)}</td>\n"
          }
        }
      }
    }
  }
  buffer
end

.with_tag(tag, buffer, indent = 0) ⇒ Object



8
9
10
11
12
# File 'lib/dbagile/io/html.rb', line 8

def with_tag(tag, buffer, indent = 0)
  buffer << "  "*indent << "<#{tag}>\n"
  yield
  buffer << "  "*indent << "</#{tag}>\n"
end