Class: Array

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

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



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

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)



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

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.



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

def to_csv
  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.



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

def to_html(indent=0)
  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
end

#to_table(name = :ascii) ⇒ Object



193
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
# File 'lib/array/formatter.rb', line 193

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

  # compute the maximum widths and the alignment of each column
  @widths = []
  @align = []
  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 = ''
  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_ymlObject

string = ARRAY.to_yml

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



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

def to_yml
  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.



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

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