Changeset 1255

Show
Ignore:
Timestamp:
02/02/08 13:00:55 (10 months ago)
Author:
sandal
Message:

#377 much faster CSV output

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ruport/branches/1.4/lib/ruport/formatter/csv.rb

    r1226 r1255  
    2424  # <tt>:format_options</tt> A hash of FasterCSV options   
    2525  # 
     26  # <tt>:formatter</tt> An existing FasterCSV object to write to 
     27  # 
    2628  # <tt>:show_table_headers</tt> True by default 
    2729  # 
     
    3234    renders :csv, :for => [ Renderer::Row,   Renderer::Table,  
    3335                            Renderer::Group, Renderer::Grouping ] 
     36 
     37    attr_writer :csv_writer 
    3438 
    3539    # Hook for setting available options using a template. See the template  
     
    4246    end 
    4347 
     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 
    4459    # Generates table header by turning column_names into a CSV row. 
    4560    # Uses the row renderer to generate the actual formatted output 
     
    4964    def build_table_header 
    5065      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 
    5268      end 
    5369    end 
     
    5773      render_data_by_row { |r|  
    5874        r.options.format_options = options.format_options 
     75        r.options.formatter = csv_writer 
    5976      } 
    6077    end 
     
    6279    # Produces CSV output for a data row. 
    6380    def build_row 
    64       require "fastercsv" 
    65       output << FCSV.generate_line(data,options.format_options || {}) 
     81      csv_writer << data 
    6682    end 
    6783     
     
    6985    #  
    7086    def build_group_header 
    71       output << data.name.to_s << "\n\n" 
     87      csv_writer << [data.name.to_s] << [] 
    7288    end 
    7389     
     
    8399    def build_grouping_header 
    84100      unless options.style == :inline 
    85         output << "#{data.grouped_by}," << grouping_columns 
     101        csv_writer << [data.grouped_by] + grouping_columns 
    86102      end 
    87103    end 
     
    102118     
    103119    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 
    106121    end 
    107122     
    108123    def render_justified_or_raw_grouping 
    109124      data.each do |_,group| 
    110         output << "#{group.name}" if options.style == :justified 
     125        prefix = [group.name.to_s] 
    111126        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 
    113129        end 
    114         output << "\n" 
     130        csv_writer << [] 
    115131      end 
    116132    end 
  • ruport/trunk/lib/ruport/formatter/csv.rb

    r1226 r1255  
    2424  # <tt>:format_options</tt> A hash of FasterCSV options   
    2525  # 
     26  # <tt>:formatter</tt> An existing FasterCSV object to write to 
     27  # 
    2628  # <tt>:show_table_headers</tt> True by default 
    2729  # 
     
    3234    renders :csv, :for => [ Renderer::Row,   Renderer::Table,  
    3335                            Renderer::Group, Renderer::Grouping ] 
     36 
     37    attr_writer :csv_writer 
    3438 
    3539    # Hook for setting available options using a template. See the template  
     
    4246    end 
    4347 
     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 
    4459    # Generates table header by turning column_names into a CSV row. 
    4560    # Uses the row renderer to generate the actual formatted output 
     
    4964    def build_table_header 
    5065      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 
    5268      end 
    5369    end 
     
    5773      render_data_by_row { |r|  
    5874        r.options.format_options = options.format_options 
     75        r.options.formatter = csv_writer 
    5976      } 
    6077    end 
     
    6279    # Produces CSV output for a data row. 
    6380    def build_row 
    64       require "fastercsv" 
    65       output << FCSV.generate_line(data,options.format_options || {}) 
     81      csv_writer << data 
    6682    end 
    6783     
     
    6985    #  
    7086    def build_group_header 
    71       output << data.name.to_s << "\n\n" 
     87      csv_writer << [data.name.to_s] << [] 
    7288    end 
    7389     
     
    8399    def build_grouping_header 
    84100      unless options.style == :inline 
    85         output << "#{data.grouped_by}," << grouping_columns 
     101        csv_writer << [data.grouped_by] + grouping_columns 
    86102      end 
    87103    end 
     
    102118     
    103119    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 
    106121    end 
    107122     
    108123    def render_justified_or_raw_grouping 
    109124      data.each do |_,group| 
    110         output << "#{group.name}" if options.style == :justified 
     125        prefix = [group.name.to_s] 
    111126        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 
    113129        end 
    114         output << "\n" 
     130        csv_writer << [] 
    115131      end 
    116132    end