Advertisement
Guest User

Untitled

a guest
Apr 15th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.79 KB | None | 0 0
  1.  #-----------------------------------------------------------------------
  2.     # to_report
  3.     #-----------------------------------------------------------------------
  4.     def to_report(recs_per_page=0, print_rec_sep=false)
  5.         result = collect { |r| @filter.collect {|f| r.send(f)} }
  6.  
  7.         # How many records before a formfeed.
  8.         delim = ' | '
  9.  
  10.         # columns of physical rows
  11.         columns = [@filter].concat(result).transpose
  12.  
  13.         max_widths = columns.collect { |c|
  14.             c.max { |a,b| a.to_s.length <=> b.to_s.length }.to_s.length
  15.         }
  16.  
  17.         row_dashes = '-' * (max_widths.inject {|sum, n| sum + n} +
  18.          delim.length * (max_widths.size - 1))
  19.  
  20.         justify_hash = { :String => :ljust, :Integer => :rjust,
  21.          :Float => :rjust, :Boolean => :ljust, :Date => :ljust,
  22.          :Time => :ljust, :DateTime => :ljust }
  23.  
  24.         header_line = @filter.zip(max_widths, @filter.collect { |f|
  25.             @filter_types[@filter.index(f)] }).collect { |x,y,z|
  26.                  x.to_s.send(justify_hash[z], y) }.join(delim)
  27.  
  28.         output = ''
  29.         recs_on_page_cnt = 0
  30.  
  31.         result.each do |row|
  32.             if recs_on_page_cnt == 0
  33.                 output << header_line + "\n" << row_dashes + "\n"
  34.             end
  35.  
  36.             output << row.zip(max_widths, @filter.collect { |f|
  37.                 @filter_types[@filter.index(f)] }).collect { |x,y,z|
  38.                     x.to_s.send(justify_hash[z], y) }.join(delim) + "\n"
  39.  
  40.             output << row_dashes + '\n' if print_rec_sep
  41.             recs_on_page_cnt += 1
  42.  
  43.             if recs_per_page > 0 and (recs_on_page_cnt ==
  44.              num_recs_per_page)
  45.                 output << '\f'
  46.                 recs_on_page_count = 0
  47.             end
  48.         end
  49.         return output
  50.     end
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement