Top Level Namespace

Defined Under Namespace

Classes: Array, String

Instance Method Summary collapse

Instance Method Details

#tabulate(labels, data, opts = { }) ⇒ Object

tabulate arrays, styles can be simple/legacy data may come in in a format like

+labels+    table heading, an array
+data+      an array of data to be tabulated, one element for one row
+opts+      optional arguments, :indent => 0, :style => 'fancy'

return a String

[   [a, b, c],  [d, e, [f, g]] .... ]
[ a, b, c] will be used as a single row
and [d, e,[f,g]] will be used as two rows


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/tabulate.rb', line 62

def tabulate(labels, data, opts = { } )
    raise 'Label and data do not have equal columns!' unless labels.size == data.transpose.size

    opts = { :indent => 0, :style => 'fancy'}.merge opts
    indent = opts[:indent]
    style = opts[:style]
    raise "Invalid table style!"    unless  $template.keys.include? style

    data = data.inject([]){|rs, r| rs += r.to_rows }
    data = data.unshift(labels).transpose
    padding = $template[style][:padding] 
    data = data.collect {|c| 
        c.collect {|e|  ' ' * padding + e.to_s + ' ' * padding } 
    }
    widths = data.collect {|c| c.collect {|a| a.width}.max } 
    newdata = []
    data.each_with_index {|c,i|
        newdata << c.collect { |e| e + ' '*(widths[i] - e.width) }
    }
    data = newdata
    data = data.transpose
    data = [ $template[style][:hlframe] + data[0].join($template[style][:fs]) + $template[style][:hrframe] ] + \
        data[1..-1].collect {|l| $template[style][:lframe] + l.join($template[style][:fs]) + $template[style][:rframe] }
    lines = []

    #add top frame
    if !$template[style][:tframe].to_s.empty?
        lines << $template[style][:cross] + widths.collect{|n| $template[style][:tframe] *n }.join($template[style][:cross]) + $template[style][:cross]
    end

    #add title 
    lines << data[0]

    #add title ruler
    if !$template[style][:hs].to_s.empty? and !$template[style][:lframe].to_s.empty?
        lines << $template[style][:cross] + widths.collect{|n| $template[style][:hs] *n }.join($template[style][:cross]) + $template[style][:cross]
    elsif !$template[style][:hs].to_s.empty?
        lines << widths.collect{|n| $template[style][:hs] *n }.join($template[style][:cross])
    end

    #add data
    data[1..-2].each{ |line|
        lines << line
        if !$template[style][:rs].to_s.empty?
            lines << $template[style][:cross] + widths.collect{|n| $template[style][:rs] *n }.join($template[style][:cross]) + $template[style][:cross]
        end
    }

    #add last record and bottom frame
    lines << data[-1]
    if !$template[style][:bframe].to_s.empty?
        lines << $template[style][:cross] + widths.collect{|n| $template[style][:bframe] *n }.join($template[style][:cross]) + $template[style][:cross]
    end

    #add indent
    lines.collect {|l| ' '*indent + l}.join("\n")
end