Changeset 1255
- Timestamp:
- 02/02/08 13:00:55 (10 months ago)
- Files:
-
- ruport/branches/1.4/lib/ruport/formatter/csv.rb (modified) (9 diffs)
- ruport/trunk/lib/ruport/formatter/csv.rb (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
ruport/branches/1.4/lib/ruport/formatter/csv.rb
r1226 r1255 24 24 # <tt>:format_options</tt> A hash of FasterCSV options 25 25 # 26 # <tt>:formatter</tt> An existing FasterCSV object to write to 27 # 26 28 # <tt>:show_table_headers</tt> True by default 27 29 # … … 32 34 renders :csv, :for => [ Renderer::Row, Renderer::Table, 33 35 Renderer::Group, Renderer::Grouping ] 36 37 attr_writer :csv_writer 34 38 35 39 # Hook for setting available options using a template. See the template … … 42 46 end 43 47 48 # Returns the current FCSV object or creates a new one if it has not 49 # been set yet. Note that FCSV(sig) has a cache and returns the *same* 50 # FCSV object if writing to the same underlying output with the same 51 # options. 52 # 53 def csv_writer 54 require 'fastercsv' 55 @csv_writer ||= options.formatter || 56 FCSV(output, options.format_options || {}) 57 end 58 44 59 # Generates table header by turning column_names into a CSV row. 45 60 # Uses the row renderer to generate the actual formatted output … … 49 64 def build_table_header 50 65 unless data.column_names.empty? || !options.show_table_headers 51 render_row data.column_names, :format_options => options.format_options 66 render_row data.column_names, :format_options => options.format_options, 67 :formatter => csv_writer 52 68 end 53 69 end … … 57 73 render_data_by_row { |r| 58 74 r.options.format_options = options.format_options 75 r.options.formatter = csv_writer 59 76 } 60 77 end … … 62 79 # Produces CSV output for a data row. 63 80 def build_row 64 require "fastercsv" 65 output << FCSV.generate_line(data,options.format_options || {}) 81 csv_writer << data 66 82 end 67 83 … … 69 85 # 70 86 def build_group_header 71 output << data.name.to_s << "\n\n"87 csv_writer << [data.name.to_s] << [] 72 88 end 73 89 … … 83 99 def build_grouping_header 84 100 unless options.style == :inline 85 output << "#{data.grouped_by}," <<grouping_columns101 csv_writer << [data.grouped_by] + grouping_columns 86 102 end 87 103 end … … 102 118 103 119 def grouping_columns 104 require "fastercsv" 105 data.data.to_a[0][1].column_names.to_csv 120 data.data.to_a[0][1].column_names 106 121 end 107 122 108 123 def render_justified_or_raw_grouping 109 124 data.each do |_,group| 110 output << "#{group.name}" if options.style == :justified125 prefix = [group.name.to_s] 111 126 group.each do |row| 112 output << "#{group.name if options.style == :raw}," << row.to_csv 127 csv_writer << prefix + row.to_a 128 prefix = [nil] if options.style == :justified 113 129 end 114 output << "\n"130 csv_writer << [] 115 131 end 116 132 end ruport/trunk/lib/ruport/formatter/csv.rb
r1226 r1255 24 24 # <tt>:format_options</tt> A hash of FasterCSV options 25 25 # 26 # <tt>:formatter</tt> An existing FasterCSV object to write to 27 # 26 28 # <tt>:show_table_headers</tt> True by default 27 29 # … … 32 34 renders :csv, :for => [ Renderer::Row, Renderer::Table, 33 35 Renderer::Group, Renderer::Grouping ] 36 37 attr_writer :csv_writer 34 38 35 39 # Hook for setting available options using a template. See the template … … 42 46 end 43 47 48 # Returns the current FCSV object or creates a new one if it has not 49 # been set yet. Note that FCSV(sig) has a cache and returns the *same* 50 # FCSV object if writing to the same underlying output with the same 51 # options. 52 # 53 def csv_writer 54 require 'fastercsv' 55 @csv_writer ||= options.formatter || 56 FCSV(output, options.format_options || {}) 57 end 58 44 59 # Generates table header by turning column_names into a CSV row. 45 60 # Uses the row renderer to generate the actual formatted output … … 49 64 def build_table_header 50 65 unless data.column_names.empty? || !options.show_table_headers 51 render_row data.column_names, :format_options => options.format_options 66 render_row data.column_names, :format_options => options.format_options, 67 :formatter => csv_writer 52 68 end 53 69 end … … 57 73 render_data_by_row { |r| 58 74 r.options.format_options = options.format_options 75 r.options.formatter = csv_writer 59 76 } 60 77 end … … 62 79 # Produces CSV output for a data row. 63 80 def build_row 64 require "fastercsv" 65 output << FCSV.generate_line(data,options.format_options || {}) 81 csv_writer << data 66 82 end 67 83 … … 69 85 # 70 86 def build_group_header 71 output << data.name.to_s << "\n\n"87 csv_writer << [data.name.to_s] << [] 72 88 end 73 89 … … 83 99 def build_grouping_header 84 100 unless options.style == :inline 85 output << "#{data.grouped_by}," <<grouping_columns101 csv_writer << [data.grouped_by] + grouping_columns 86 102 end 87 103 end … … 102 118 103 119 def grouping_columns 104 require "fastercsv" 105 data.data.to_a[0][1].column_names.to_csv 120 data.data.to_a[0][1].column_names 106 121 end 107 122 108 123 def render_justified_or_raw_grouping 109 124 data.each do |_,group| 110 output << "#{group.name}" if options.style == :justified125 prefix = [group.name.to_s] 111 126 group.each do |row| 112 output << "#{group.name if options.style == :raw}," << row.to_csv 127 csv_writer << prefix + row.to_a 128 prefix = [nil] if options.style == :justified 113 129 end 114 output << "\n"130 csv_writer << [] 115 131 end 116 132 end
