Sha256: 407524c68df369fd4822490677a544dda4abd72539432d52de9700008cbf82b0

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

require 'text-table'
class ReportBuilder
  class Table
    # Text Builder for ReportBuilder::Table objects.
    # 
    # Uses Aaron Tinio's text-table gem[http://github.com/aptinio/text-table]
    
    class TextBuilder < ElementBuilder
      def generate()
        t=@element
        @rowspans=[]
        @builder.text(t.name)
        return if t.header.size+t.rows.size==0
        table = Text::Table.new do |tt|
          tt.head=t.header if t.header.size>0
          tt.rows=t.rows.map {|row| parse_row(row)}
        end
        #pp table.rows
        @builder.preformatted(table.to_s)
      end
      # Parse a row
      def parse_row(row)
        return :separator if row==:hr
        t=@element
        row_ary=[]
        real_i=0
        row.each_index do |i|
          extra=1
          
          while !@rowspans[real_i].nil? and @rowspans[real_i]>0
            @rowspans[real_i]-=1
            row_ary << ""
            real_i+=1
          end
          
          if row[i].is_a? ReportBuilder::Table::Rowspan
            @rowspans[real_i]=row[i].rows-1
            row_ary << row[i].to_s
          elsif row[i].is_a? ReportBuilder::Table::Colspan
            row_ary.push({:value=>row[i].to_s, :colspan=>row[i].cols})
            extra=row[i].cols
          elsif row[i].nil?
            row_ary.push("")
          else
            #size=row[i].to_s.size
            #puts sprintf("%i : %s (%d-%d)",i,row[i].to_s,@max_cols[i], size)
            row_ary.push(row[i].to_s)
          end
          real_i+=extra
        end
        row_ary
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
reportbuilder-1.4.2 lib/reportbuilder/table/textbuilder.rb
reportbuilder-1.4.1 lib/reportbuilder/table/textbuilder.rb
reportbuilder-1.4.0 lib/reportbuilder/table/textbuilder.rb
reportbuilder-1.3.0 lib/reportbuilder/table/textbuilder.rb
reportbuilder-1.2.5 lib/reportbuilder/table/textbuilder.rb
reportbuilder-1.2.4 lib/reportbuilder/table/textbuilder.rb
reportbuilder-1.2.3 lib/reportbuilder/table/textbuilder.rb