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 ARRAY.to_yaml – format an array of arrays as a YAML 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



251
252
253
254
255
256
257
258
259
# File 'lib/array/formatter.rb', line 251

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)



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

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.



56
57
58
59
60
61
62
# File 'lib/array/formatter.rb', line 56

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.



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

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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/array/formatter.rb', line 194

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

#to_yamlObject

string = ARRAY.to_yaml

Convert an array of arrays to YAML (using built-in core methods)



68
69
70
71
# File 'lib/array/formatter.rb', line 68

def to_yaml
  require 'yaml'
  YAML.dump(self)
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.



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

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