Changeset 570
- Timestamp:
- 03/01/07 21:30:15 (2 years ago)
- Files:
-
- trunk/lib/ruport/format/csv.rb (modified) (4 diffs)
- trunk/lib/ruport/format/html.rb (modified) (4 diffs)
- trunk/lib/ruport/format/plugin.rb (modified) (1 diff)
- trunk/lib/ruport/format/text.rb (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/ruport/format/csv.rb
r562 r570 4 4 # See also: Renderer::Table 5 5 class CSV < Plugin 6 7 opt_reader :show_table_headers, :format_options 8 6 9 # Generates table header by turning column_names into a CSV row. 7 10 # Uses the row renderer to generate the actual formatted output … … 10 13 # or the Data::Table has no column names. 11 14 def build_table_header 12 unless data.column_names.empty? || ! options.show_table_headers15 unless data.column_names.empty? || !show_table_headers 13 16 Ruport::Renderer::Row.render_csv data.column_names, :io => output, 14 :format_options => options.format_options17 :format_options => format_options 15 18 end 16 19 end … … 19 22 def build_table_body 20 23 render_data_by_row { |r| 21 r.options.format_options = options.format_options24 r.options.format_options = format_options 22 25 } 23 26 end … … 26 29 def build_row 27 30 require "fastercsv" 28 FCSV(output, options.format_options || {}) { |csv|31 FCSV(output,format_options || {}) { |csv| 29 32 csv << data 30 33 } trunk/lib/ruport/format/html.rb
r559 r570 4 4 # See also Renderer::Table 5 5 class HTML < Plugin 6 7 opt_reader :show_table_headers, :class_str 6 8 7 9 # Generates table headers based on the column names of your Data::Table. … … 11 13 def build_table_header 12 14 output << "\t<table>\n" 13 unless data.column_names.empty? || ! options.show_table_headers15 unless data.column_names.empty? || !show_table_headers 14 16 output << "\t\t<tr>\n\t\t\t<th>" + 15 17 data.column_names.join("</th>\n\t\t\t<th>") + … … 30 32 31 33 rend.options.class_str = classstr 32 #Ruport::Renderer::Row.render_html(33 # :class_str => classstr, :io => output) { |rend| rend.data = row }34 34 end 35 35 end … … 43 43 def build_row 44 44 output << 45 "\t\t<tr#{ options.class_str}>\n\t\t\t<td#{options.class_str}>" +46 data.to_a.join("</td>\n\t\t\t<td#{ options.class_str}>") +45 "\t\t<tr#{class_str}>\n\t\t\t<td#{class_str}>" + 46 data.to_a.join("</td>\n\t\t\t<td#{class_str}>") + 47 47 "</td>\n\t\t</tr>\n" 48 48 end trunk/lib/ruport/format/plugin.rb
r559 r570 20 20 end 21 21 22 module OptionAccessors 23 24 25 module ClassMethods 26 def opt_reader(*opts) 27 require "forwardable" 28 extend Forwardable 29 opts.each { |o| def_delegator :@options, o } 30 end 31 end 32 33 def self.included(base) 34 base.extend(ClassMethods) 35 end 36 end 37 22 38 class Plugin 39 23 40 include RenderingTools 41 include OptionAccessors 24 42 25 43 attr_accessor :options trunk/lib/ruport/format/text.rb
r559 r570 2 2 module Format 3 3 class Text < Plugin 4 5 opt_reader :max_col_width, :alignment, :table_width, :show_table_headers 4 6 5 7 # Checks to ensure the table is not empty and then calls … … 19 21 20 22 c = data.column_names.enum_for(:each_with_index).map { |f,i| 21 f.to_s.center( options.max_col_width[i])23 f.to_s.center(max_col_width[i]) 22 24 } 23 25 … … 29 31 # 30 32 def should_render_column_names 31 not data.column_names.empty? || ! options.show_table_headers33 not data.column_names.empty? || !show_table_headers 32 34 end 33 35 … … 43 45 44 46 45 calculate_max_col_widths unless options.max_col_width47 calculate_max_col_widths unless max_col_width 46 48 47 49 render_data_by_row do |rend| 48 50 rend.options do |o| 49 o.max_col_width = options.max_col_width50 o.alignment = options.alignment51 o.table_width = options.table_width51 o.max_col_width = max_col_width 52 o.alignment = alignment 53 o.table_width = table_width 52 54 end 53 55 end … … 58 60 def build_row 59 61 60 max_col_widths_for_row(data) unless options.max_col_width62 max_col_widths_for_row(data) unless max_col_width 61 63 62 64 data.enum_for(:each_with_index).inject(line=[]) { |s,e| 63 65 field,index = e 64 if options.alignment.eql? :center65 line << field.to_s.center( options.max_col_width[index])66 if alignment.eql? :center 67 line << field.to_s.center(max_col_width[index]) 66 68 else 67 69 align = field.is_a?(Numeric) ? :rjust : :ljust 68 line << field.to_s.send(align, options.max_col_width[index])70 line << field.to_s.send(align, max_col_width[index]) 69 71 end 70 72 } … … 77 79 # "+------------------+" 78 80 def hr 79 len = options.max_col_width.inject(data[0].to_a.length * 3) {|s,e|s+e}+181 len = max_col_width.inject(data[0].to_a.length * 3) {|s,e|s+e}+1 80 82 "+" + "-"*(len-2) + "+\n" 81 83 end … … 86 88 def width 87 89 require "ruport/system_extensions" 88 options.table_width || SystemExtensions.terminal_width90 table_width || SystemExtensions.terminal_width 89 91 end 90 92 … … 103 105 104 106 # allow override 105 return if options.max_col_width107 return if max_col_width 106 108 107 109 options.max_col_width = [] … … 109 111 unless data.column_names.empty? 110 112 data.column_names.each_index do |i| 111 options.max_col_width[i] = data.column_names[i].to_s.length113 max_col_width[i] = data.column_names[i].to_s.length 112 114 end 113 115 end … … 120 122 options.max_col_width ||= [] 121 123 row.each_with_index do |f,i| 122 if ! options.max_col_width[i] || f.to_s.length > options.max_col_width[i]123 options.max_col_width[i] = f.to_s.length124 if !max_col_width[i] || f.to_s.length > max_col_width[i] 125 max_col_width[i] = f.to_s.length 124 126 end 125 127 end
