Changeset 1271
- Timestamp:
- 02/10/08 08:37:52 (9 months ago)
- Files:
-
- ruport/branches/brian/exp-option-formatter/lib/ruport/formatter.rb (modified) (1 diff)
- ruport/branches/brian/exp-option-formatter/lib/ruport/formatter/csv.rb (modified) (3 diffs)
- ruport/branches/brian/exp-option-formatter/lib/ruport/formatter/html.rb (modified) (1 diff)
- ruport/branches/brian/exp-option-formatter/lib/ruport/formatter/pdf.rb (modified) (5 diffs)
- ruport/branches/brian/exp-option-formatter/lib/ruport/formatter/text.rb (modified) (1 diff)
- ruport/branches/brian/exp-option-formatter/lib/ruport/renderer.rb (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
ruport/branches/brian/exp-option-formatter/lib/ruport/formatter.rb
r1262 r1271 206 206 # modify the data object may wish to override this. 207 207 def data=(val) 208 @data = val .dup208 @data = val && val.dup 209 209 end 210 210 ruport/branches/brian/exp-option-formatter/lib/ruport/formatter/csv.rb
r1260 r1271 24 24 # <tt>:format_options</tt> A hash of FasterCSV options 25 25 # 26 # <tt>: formatter</tt> An existing FasterCSV object to write to26 # <tt>:csv_writer</tt> An existing FasterCSV object to write to 27 27 # 28 28 # <tt>:show_table_headers</tt> True by default … … 56 56 # 57 57 def csv_writer 58 @csv_writer ||= options. formatter ||58 @csv_writer ||= options.csv_writer || 59 59 FCSV(output, options.format_options || {}) 60 60 end … … 68 68 unless data.column_names.empty? || !options.show_table_headers 69 69 render_row data.column_names, :format_options => options.format_options, 70 : formatter => csv_writer70 :csv_writer => csv_writer 71 71 end 72 72 end ruport/branches/brian/exp-option-formatter/lib/ruport/formatter/html.rb
r1226 r1271 52 52 # Replaces nil and empty strings with " " 53 53 def build_table_body 54 render_data_by_row do |rend|54 render_data_by_row(options) do |rend| 55 55 r = rend.data 56 56 rend.data = r.map { |e| e.to_s.empty? ? " " : e } ruport/branches/brian/exp-option-formatter/lib/ruport/formatter/pdf.rb
r1236 r1271 87 87 # 88 88 def pdf_writer 89 @pdf_writer ||= options. formatter ||89 @pdf_writer ||= options.pdf_writer || 90 90 ::PDF::Writer.new( :paper => options.paper_size || "LETTER", 91 91 :orientation => options.paper_orientation || :portrait) … … 112 112 # Renders the group as a table for Renderer::Group. 113 113 def build_group_body 114 render_table data, options .to_hash.merge(:formatter => pdf_writer)114 render_table data, options 115 115 end 116 116 … … 120 120 case options.style 121 121 when :inline 122 render_inline_grouping(options.to_hash.merge( :formatter => pdf_writer,122 render_inline_grouping(options.to_hash.merge( 123 123 :skip_finalize_table => true)) 124 124 when :justified, :separated … … 452 452 table << [" "] if options.style == :separated 453 453 end 454 render_table table, options .to_hash.merge(:formatter => pdf_writer)454 render_table table, options 455 455 end 456 456 … … 461 461 group.each {|r| table << r } 462 462 end 463 render_table table, options .to_hash.merge(:formatter => pdf_writer)463 render_table table, options 464 464 end 465 465 ruport/branches/brian/exp-option-formatter/lib/ruport/formatter/text.rb
r1251 r1271 95 95 calculate_max_col_widths unless options.max_col_width 96 96 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) 105 98 106 99 output << fit_to_width(hr) ruport/branches/brian/exp-option-formatter/lib/ruport/renderer.rb
r1262 r1271 166 166 def as(format,options={}) 167 167 raise RendererNotSetError unless self.class.renderer 168 unless self.class.renderer.formats.include?(format) 168 unless self.class.renderer.formats.include?(format) || format.nil? 169 169 raise UnknownFormatError 170 170 end … … 176 176 end 177 177 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 178 184 179 185 def save_as(file,options={}) … … 331 337 # Please see the examples/ directory for custom renderer examples, because 332 338 # 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| 335 343 yield(r) if block_given? 336 344 r.setup if r.respond_to? :setup … … 338 346 rend.run 339 347 rend.formatter.save_output(rend.options.file) if rend.options.file 348 rend.formatter.data, rend.formatter.options = *old_settings if old_settings 340 349 return rend.formatter.output 341 350 end … … 362 371 # Returns the renderer instance. 363 372 # 364 def build(format , add_options=nil)373 def build(format=nil, add_options=nil, save_proc = nil) 365 374 rend = self.new 366 375 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 371 388 end 372 389 if add_options.kind_of?(Hash) … … 375 392 add_options.each {|k,v| rend.options.send("#{k}=",v) } 376 393 end 394 rend.options.formatter = rend.formatter 377 395 378 396 yield(rend) if block_given? … … 396 414 397 415 # The name of format being used. 398 attr_accessor :format 416 def format 417 formatter.format 418 end 399 419 400 420 # The formatter object being used.
