Changeset 1271

Show
Ignore:
Timestamp:
02/10/08 08:37:52 (9 months ago)
Author:
brian
Message:

Implement :formatter option to select an existing formatter object to use.
For more details see README.exp-option-formatter

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ruport/branches/brian/exp-option-formatter/lib/ruport/formatter.rb

    r1262 r1271  
    206206    # modify the data object may wish to override this. 
    207207    def data=(val) 
    208       @data = val.dup 
     208      @data = val && val.dup 
    209209    end 
    210210 
  • ruport/branches/brian/exp-option-formatter/lib/ruport/formatter/csv.rb

    r1260 r1271  
    2424  # <tt>:format_options</tt> A hash of FasterCSV options   
    2525  # 
    26   # <tt>:formatter</tt> An existing FasterCSV object to write to 
     26  # <tt>:csv_writer</tt> An existing FasterCSV object to write to 
    2727  # 
    2828  # <tt>:show_table_headers</tt> True by default 
     
    5656    # 
    5757    def csv_writer 
    58       @csv_writer ||= options.formatter || 
     58      @csv_writer ||= options.csv_writer || 
    5959        FCSV(output, options.format_options || {}) 
    6060    end 
     
    6868      unless data.column_names.empty? || !options.show_table_headers 
    6969        render_row data.column_names, :format_options => options.format_options, 
    70                                       :formatter => csv_writer 
     70                                      :csv_writer => csv_writer 
    7171      end 
    7272    end 
  • ruport/branches/brian/exp-option-formatter/lib/ruport/formatter/html.rb

    r1226 r1271  
    5252    # Replaces nil and empty strings with "&nbsp;"  
    5353    def build_table_body 
    54       render_data_by_row do |rend| 
     54      render_data_by_row(options) do |rend| 
    5555        r = rend.data 
    5656        rend.data = r.map { |e| e.to_s.empty? ? "&nbsp;" : e } 
  • ruport/branches/brian/exp-option-formatter/lib/ruport/formatter/pdf.rb

    r1236 r1271  
    8787    #     
    8888    def pdf_writer 
    89       @pdf_writer ||= options.formatter || 
     89      @pdf_writer ||= options.pdf_writer || 
    9090        ::PDF::Writer.new( :paper => options.paper_size || "LETTER", 
    9191              :orientation => options.paper_orientation || :portrait) 
     
    112112    # Renders the group as a table for Renderer::Group. 
    113113    def build_group_body 
    114       render_table data, options.to_hash.merge(:formatter => pdf_writer) 
     114      render_table data, options 
    115115    end 
    116116                      
     
    120120      case options.style 
    121121      when :inline 
    122         render_inline_grouping(options.to_hash.merge(:formatter => pdf_writer, 
     122        render_inline_grouping(options.to_hash.merge( 
    123123            :skip_finalize_table => true)) 
    124124      when :justified, :separated 
     
    452452        table << [" "] if options.style == :separated 
    453453      end 
    454       render_table table, options.to_hash.merge(:formatter => pdf_writer) 
     454      render_table table, options 
    455455    end 
    456456     
     
    461461        group.each {|r| table << r } 
    462462      end 
    463       render_table table, options.to_hash.merge(:formatter => pdf_writer) 
     463      render_table table, options 
    464464    end 
    465465     
  • ruport/branches/brian/exp-option-formatter/lib/ruport/formatter/text.rb

    r1251 r1271  
    9595      calculate_max_col_widths unless options.max_col_width 
    9696 
    97       render_data_by_row do |rend| 
    98         rend.options do |o| 
    99           o.max_col_width = options.max_col_width 
    100           o.alignment     = options.alignment 
    101           o.table_width   = options.table_width    
    102           o.ignore_table_width = options.ignore_table_width 
    103         end 
    104       end 
     97      render_data_by_row(options) 
    10598 
    10699      output << fit_to_width(hr) 
  • ruport/branches/brian/exp-option-formatter/lib/ruport/renderer.rb

    r1262 r1271  
    166166    def as(format,options={}) 
    167167      raise RendererNotSetError unless self.class.renderer 
    168       unless self.class.renderer.formats.include?(format) 
     168      unless self.class.renderer.formats.include?(format) || format.nil? 
    169169        raise UnknownFormatError 
    170170      end 
     
    176176      end 
    177177    end       
     178     
     179    # If you have an existing :formatter in your options, then you don't 
     180    # need to specify the format     
     181    def format(options) 
     182      as(nil, options) 
     183    end 
    178184     
    179185    def save_as(file,options={}) 
     
    331337    # Please see the examples/ directory for custom renderer examples, because 
    332338    # this is not nearly as complicated as it sounds in most cases. 
    333     def render(format, add_options=nil) 
    334       rend = build(format, add_options) { |r| 
     339    def render(format=nil, add_options=nil) 
     340      old_settings = nil 
     341      saver = proc { |f| *old_settings = f.data, f.options } 
     342      rend = build(format, add_options, saver) { |r| 
    335343          yield(r) if block_given?    
    336344        r.setup if r.respond_to? :setup 
     
    338346      rend.run 
    339347      rend.formatter.save_output(rend.options.file) if rend.options.file 
     348      rend.formatter.data, rend.formatter.options = *old_settings if old_settings 
    340349      return rend.formatter.output 
    341350    end 
     
    362371    # Returns the renderer instance. 
    363372    # 
    364     def build(format, add_options=nil) 
     373    def build(format=nil, add_options=nil, save_proc = nil) 
    365374      rend = self.new 
    366375 
    367       rend.send(:use_formatter, format) 
    368       rend.send(:options=, options.dup) 
    369       if rend.class.const_defined? :Helpers 
    370         rend.formatter.extend(rend.class.const_get(:Helpers)) 
     376      if add_options && (of = add_options[:formatter]) && 
     377                        (format.nil? || of.format == format) 
     378        save_proc[of] if save_proc 
     379        rend.formatter = of 
     380        rend.send(:options=, options.dup) 
     381        of.options.each {|k,v| rend.options.send("#{k}=",v) } 
     382      else 
     383        rend.send(:use_formatter, format) 
     384        rend.send(:options=, options.dup) 
     385        if rend.class.const_defined? :Helpers 
     386          rend.formatter.extend(rend.class.const_get(:Helpers)) 
     387        end 
    371388      end 
    372389      if add_options.kind_of?(Hash) 
     
    375392        add_options.each {|k,v| rend.options.send("#{k}=",v) } 
    376393      end 
     394      rend.options.formatter = rend.formatter 
    377395 
    378396      yield(rend) if block_given? 
     
    396414   
    397415  # The name of format being used. 
    398   attr_accessor :format   
     416  def format 
     417    formatter.format 
     418  end 
    399419   
    400420  # The formatter object being used.