Sha256: 421b09f633186c2b6c0769663ee9bfad9180f46f42637df1d903ae2ea829b35a

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Tableview::Ouput
  class HTML
    def process(tv)
      @table = ""
      @p = 0
      tag :table, tv.options do
        
        tv.parts.each do |part|
          tag (case part.class.to_s
          when "Tableview::ViewHandler::Header"
            header = true
            :thead
          when "Tableview::ViewHandler::Footer"
            :tfoot
          when "Tableview::ViewHandler::Body"
            :tbody
          end), part.options do 
        
            part.rows.each do |row|
              tag :tr, row.options do
                row.cells.each do |cell|
                  tag( header || cell.options[:header] ? :th : :td, cell.options) do 
                    @table += ERB::Util::html_escape(cell.contents.to_s)
                  end #td / #th
                end
              end #tr
            end

          end #t{part}
        end
      end #table
    end
    
    def tag(tag, attributes = {})
      atrs = attributes.map {|k,v| " #{k}=\"#{v}\"" }.join
      @table += "\n#{" "*@p}<#{tag}#{atrs}>"
      @p += 2
      yield
      @p -= 2
      @table += "\n#{" " * @p}</#{tag}>"
    end
    
    def to_s
      @table.html_safe.to_s
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tableview-0.2.0 lib/tableview/output/html.rb
tableview-0.1.0 lib/tableview/output/html.rb