Changeset 1224
- Timestamp:
- 12/09/07 20:32:17 (1 year ago)
- Files:
-
- ruport/trunk/examples/pdf_report_with_common_base.rb (modified) (2 diffs)
- ruport/trunk/examples/png_embed.rb (modified) (3 diffs)
- ruport/trunk/lib/ruport/formatter.rb (modified) (1 diff)
- ruport/trunk/lib/ruport/formatter/csv.rb (modified) (7 diffs)
- ruport/trunk/lib/ruport/formatter/html.rb (modified) (3 diffs)
- ruport/trunk/lib/ruport/formatter/pdf.rb (modified) (6 diffs)
- ruport/trunk/lib/ruport/formatter/text.rb (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
ruport/trunk/examples/pdf_report_with_common_base.rb
r1210 r1224 23 23 def setup 24 24 data.rename_columns { |c| c.to_s.titleize } 25 # this lets us omit the options prefix in the formatter26 formatter.class.opt_reader(:example)27 25 end 28 26 end … … 58 56 def build_client_header 59 57 pad(10) do 60 add_text "Specific Report Header with example=#{ example}",58 add_text "Specific Report Header with example=#{options.example}", 61 59 :justification => :center, :font_size => 12 62 60 end ruport/trunk/examples/png_embed.rb
r1210 r1224 10 10 11 11 renders :html, :for => RoadmapRenderer 12 opt_reader :image_file13 12 14 13 def layout … … 19 18 20 19 def build_roadmap_image 21 output << "<img src='#{ image_file}'/>"20 output << "<img src='#{options.image_file}'/>" 22 21 end 23 22 … … 31 30 32 31 renders :pdf, :for => RoadmapRenderer 33 opt_reader :image_file34 32 35 33 def build_roadmap_image 36 center_image_in_box image_file, :x => 0, :y => 200,34 center_image_in_box options.image_file, :x => 0, :y => 200, 37 35 :width => 624, :height => 432 38 36 move_cursor_to 80 ruport/trunk/lib/ruport/formatter.rb
r1223 r1224 181 181 end 182 182 183 # Allows the options specified to be accessed directly.184 #185 # opt_reader :something186 # something == options.something #=> true187 def self.opt_reader(*opts)188 require "forwardable"189 extend Forwardable190 opts.each { |o| def_delegator :@options, o }191 end192 193 183 # Gives a list of formats registered for this formatter. 194 184 def self.formats ruport/trunk/lib/ruport/formatter/csv.rb
r1188 r1224 33 33 Renderer::Group, Renderer::Grouping ] 34 34 35 opt_reader :show_table_headers,36 :format_options,37 :show_group_headers,38 :style39 40 35 # Hook for setting available options using a template. See the template 41 36 # documentation for the available options and their format. … … 53 48 # or the Data::Table has no column names. 54 49 def build_table_header 55 unless data.column_names.empty? || ! show_table_headers56 render_row data.column_names, :format_options => format_options50 unless data.column_names.empty? || !options.show_table_headers 51 render_row data.column_names, :format_options => options.format_options 57 52 end 58 53 end … … 61 56 def build_table_body 62 57 render_data_by_row { |r| 63 r.options.format_options = format_options58 r.options.format_options = options.format_options 64 59 } 65 60 end … … 68 63 def build_row 69 64 require "fastercsv" 70 output << FCSV.generate_line(data, format_options || {})65 output << FCSV.generate_line(data,options.format_options || {}) 71 66 end 72 67 … … 87 82 # 88 83 def build_grouping_header 89 unless style == :inline84 unless options.style == :inline 90 85 output << "#{data.grouped_by}," << grouping_columns 91 86 end … … 94 89 # Determines the proper style to use and renders the Grouping. 95 90 def build_grouping_body 96 case style91 case options.style 97 92 when :inline 98 93 render_inline_grouping(options) … … 113 108 def render_justified_or_raw_grouping 114 109 data.each do |_,group| 115 output << "#{group.name}" if style == :justified110 output << "#{group.name}" if options.style == :justified 116 111 group.each do |row| 117 output << "#{group.name if style == :raw}," << row.to_csv112 output << "#{group.name if options.style == :raw}," << row.to_csv 118 113 end 119 114 output << "\n" ruport/trunk/lib/ruport/formatter/html.rb
r1188 r1224 29 29 Renderer::Group, Renderer::Grouping ] 30 30 31 opt_reader :show_table_headers, :show_group_headers, :style32 33 31 # Hook for setting available options using a template. See the template 34 32 # documentation for the available options and their format. … … 44 42 def build_table_header 45 43 output << "\t<table>\n" 46 unless data.column_names.empty? || ! show_table_headers44 unless data.column_names.empty? || !options.show_table_headers 47 45 output << "\t\t<tr>\n\t\t\t<th>" + 48 46 data.column_names.join("</th>\n\t\t\t<th>") + … … 90 88 # 91 89 def build_grouping_body 92 case style90 case options.style 93 91 when :inline 94 92 render_inline_grouping(options) ruport/trunk/lib/ruport/formatter/pdf.rb
r1221 r1224 47 47 attr_writer :pdf_writer 48 48 49 opt_reader :style,50 :table_format,51 :text_format,52 :paper_size,53 :paper_orientation54 55 49 save_as_binary_file 56 50 … … 78 72 def pdf_writer 79 73 @pdf_writer ||= options.formatter || 80 ::PDF::Writer.new( :paper => paper_size || "LETTER",81 :orientation => paper_orientation || :portrait)74 ::PDF::Writer.new( :paper => options.paper_size || "LETTER", 75 :orientation => options.paper_orientation || :portrait) 82 76 end 83 77 … … 108 102 # Renderer::Grouping. 109 103 def build_grouping_body 110 case style104 case options.style 111 105 when :inline 112 106 render_inline_grouping(options.to_hash.merge(:formatter => pdf_writer, … … 136 130 # add_text("Hello Mike",:font_size => 16) # renders at 16pt 137 131 def add_text(text, format_opts={}) 138 format_opts = text_format.merge(format_opts) iftext_format132 format_opts = options.text_format.merge(format_opts) if options.text_format 139 133 pdf_writer.text(text, format_opts) 140 134 end … … 265 259 table_data.rename_columns { |c| c.to_s } 266 260 267 if table_format 268 format_opts = Marshal.load(Marshal.dump(table_format.merge(format_opts))) 261 if options.table_format 262 format_opts = 263 Marshal.load(Marshal.dump(options.table_format.merge(format_opts))) 269 264 end 270 265 … … 430 425 end 431 426 end 432 table << [" "] if style == :separated427 table << [" "] if options.style == :separated 433 428 end 434 429 render_table table, options.to_hash.merge(:formatter => pdf_writer) ruport/trunk/lib/ruport/formatter/text.rb
r1188 r1224 49 49 Renderer::Group, Renderer::Grouping ] 50 50 51 opt_reader :max_col_width, :alignment, :table_width,52 :show_table_headers, :show_group_headers,53 :ignore_table_width54 55 51 # Hook for setting available options using a template. See the template 56 52 # documentation for the available options and their format. … … 79 75 80 76 c = data.column_names.enum_for(:each_with_index).map { |f,i| 81 f.to_s.center( max_col_width[i])77 f.to_s.center(options.max_col_width[i]) 82 78 } 83 79 … … 97 93 return if data.empty? 98 94 99 calculate_max_col_widths unless max_col_width95 calculate_max_col_widths unless options.max_col_width 100 96 101 97 render_data_by_row do |rend| 102 98 rend.options do |o| 103 o.max_col_width = max_col_width104 o.alignment = alignment105 o.table_width = table_width106 o.ignore_table_width = ignore_table_width99 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 107 103 end 108 104 end … … 120 116 # 121 117 def build_row 122 max_col_widths_for_row(data) unless max_col_width118 max_col_widths_for_row(data) unless options.max_col_width 123 119 124 120 data.enum_for(:each_with_index).inject(line=[]) { |s,e| 125 121 field,index = e 126 if alignment.eql? :center127 line << field.to_s.center( max_col_width[index])122 if options.alignment.eql? :center 123 line << field.to_s.center(options.max_col_width[index]) 128 124 else 129 125 align = field.is_a?(Numeric) ? :rjust : :ljust 130 line << field.to_s.send(align, max_col_width[index])126 line << field.to_s.send(align, options.max_col_width[index]) 131 127 end 132 128 } … … 158 154 # 159 155 def should_render_column_names? 160 not data.column_names.empty? || ! show_table_headers156 not data.column_names.empty? || !options.show_table_headers 161 157 end 162 158 … … 167 163 def hr 168 164 ref = data.column_names.empty? ? data[0].to_a : data.column_names 169 len = max_col_width.inject(ref.length * 3) {|s,e|s+e}165 len = options.max_col_width.inject(ref.length * 3) {|s,e|s+e} 170 166 "+" + "-"*(len-1) + "+\n" 171 167 end … … 175 171 # Otherwise, uses SystemExtensions to determine terminal width. 176 172 def width 177 table_width || SystemExtensions.terminal_width173 options.table_width || SystemExtensions.terminal_width 178 174 end 179 175 … … 192 188 def calculate_max_col_widths 193 189 # allow override 194 return if max_col_width190 return if options.max_col_width 195 191 196 192 options.max_col_width = [] … … 198 194 unless data.column_names.empty? 199 195 data.column_names.each_index do |i| 200 max_col_width[i] = data.column_names[i].to_s.length196 options.max_col_width[i] = data.column_names[i].to_s.length 201 197 end 202 198 end … … 210 206 options.max_col_width ||= [] 211 207 row.each_with_index do |f,i| 212 if ! max_col_width[i] || f.to_s.length >max_col_width[i]213 max_col_width[i] = f.to_s.length208 if !options.max_col_width[i] || f.to_s.length > options.max_col_width[i] 209 options.max_col_width[i] = f.to_s.length 214 210 end 215 211 end
