Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/array/formatter.rb

Overview

array-formatter.rb

Author: Alan K. Stebbens <[email protected]>

ARRAY.to_html – format an array of arrays into an HTML table string ARRAY.to_csv – format an array of arrays into a CSV string ARRAY.to_table – format an array of arrays into an ASCII table string

Defined Under Namespace

Classes: Formatter, TableChars

Instance Method Summary collapse

Instance Method Details

#table_data(data) ⇒ Object

table_data data

generate a data row as part of a table



237
238
239
240
241
242
243
244
245
# File 'lib/array/formatter.rb', line 237

def table_data data
  left, middle, right = [@chars[:ldb], @chars[:idb], @chars[:rdb]]
  a = []
  data.each_with_index do |item, x|
    a << (' ' + item.to_s.send(@align[x] || :ljust, @widths[x]) + ' ')
  end
  s = @chars.wrap(left) + a.join(@chars.wrap(middle)) + @chars.wrap(right) + "\n"
  s
end

#table_line(position) ⇒ Object

table_line position

generate a table line for position (:top, :middle, :bottom)



221
222
223
224
225
226
227
228
229
230
# File 'lib/array/formatter.rb', line 221

def table_line position
  c = @chars
  left, line, mid, right = case position
                           when :top     then [c[:tlb], c[:tb], c[:tib], c[:trb]]
                           when :middle  then [c[:lib], c[:ib], c[:mib], c[:rib]]
                           when :bottom  then [c[:blb], c[:bb], c[:bib], c[:brb]]
                           end
  s = @chars.wrap(left + @widths.map{|w| line * (w + 2)}.join(mid) + right) + "\n"
  s
end

#to_csvObject

string = ARRAY.to_csv

Convert an array of arrays to CSV representation Basically, each top-level array row becomes a line of CSV data The 2nd level arrays become individual rows of data, separated by commas. Each 2nd level array item gets quoted if it contains any punctuation characters.



55
56
57
# File 'lib/array/formatter.rb', line 55

def to_csv
  self.map {|row| row.map {|f| f =~ /[[:punct:]]/ ?  '"' + f.gsub(/"/, '""') + '"' : f }.join(",")}.join("\n")
end

#to_html(indent = 0) ⇒ Object

string = ARRAY.to_html indent=0

Return an HTML representation of an array of arrays as an HTML table. If indent given, indent the <table> by that many spaces.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/array/formatter.rb', line 18

def to_html indent=0
  require 'cgi'
  s = self.wrap_map_html_data("table", indent + 0) do |row,   rx|
       row.wrap_map_html_data("tr",    indent + 1) do |field, fx|
         tag = rx == 0 ? 'th' : 'td'
         (' ' *                       (indent + 2)) +   # indention
         "<#{tag}>"                                 +   # open tag
         (field && CGI.escapeHTML(field) || '')     +   # cell text
         "</#{tag}>\n"                                  # close tag
       end
  end
  s
end

#to_table(name = :ascii) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/array/formatter.rb', line 180

def to_table name=:ascii
  @chars = TableChars.get name

  # compute the maximum widths and the alignment of each column
  @widths = []
  @align = []
  self.each_with_index do |row,rx| 
    row.each_with_index do |col,cx|
      @widths[cx] ||= 0
      l = col.to_s.length 
      @widths[cx] = l if l > @widths[cx]
      if rx == 0
        type = nil
      else
        type = case (col || '')
               when /^\$?\s*\d[,\d]*\.\d*$/  then :rjust   # real number and/or currency
               when /^\$?\s*\d[,\d]*$/       then :rjust   # integer
               when /^$/, /^\s*-\s*$/, nil   then nil       # empty values have no alignment
               else :ljust
               end
      end
      @align[cx] ||= (col ? type : nil)
      @align[cx] = :ljust if type && @align[cx] != type && rx > 0
    end
  end

  # now format each row
  s = ''
  self.each_with_index do |row, rx|
    s << table_line(rx == 0 ? :top : :middle)
    s << table_data(row)
  end
  s << table_line(:bottom)
  s
end

#wrap_map_html_data(tag, indent = 0) ⇒ Object

string = ARRAY.wrap_map_html_data TAG, INDENT {|DATA, INDEX| block}

Return a string HTML representation of an array of data, starting with <TAG> and ending with </TAG>, and invoking the block on each array item, passing the DATA item with its INDEX.



38
39
40
41
42
43
44
45
# File 'lib/array/formatter.rb', line 38

def wrap_map_html_data tag, indent=0
  s = ''
  prefix = ' ' * indent
  s << prefix + "<#{tag}>\n"
  self.each_with_index {|data, x| s << ((yield data, x) || '') }
  s << prefix + "</#{tag}>\n"
  s
end