Class: Ruport::Formatter

Inherits:
Object
  • Object
show all
Includes:
RenderingTools
Defined in:
lib/ruport/formatter.rb

Overview

Formatter is the base class for Ruport’s format implementations.

Typically, a Formatter will implement one or more output types, and be registered with one or more Controller classes.

This class provides all the necessary base functionality to make use of Ruport’s rendering system, including option handling, data access, and basic output wrapping.

The following example should provide a general idea of how formatters work, but see the built in formatters for reference implementations.

A simple Controller definition is included to help show the example in context, but you can also build your own custom interface to formatter if you wish.

class ReverseController < Ruport::Controller
   stage :reversed_header, :reversed_body
end

class ReversedText < Ruport::Formatter

   # Hooks formatter up to controller
   renders :txt, :for => ReverseController

   # Implements ReverseController's :reversed_header hook
   # but can be used by any controller
   def build_reversed_header
      output << "#{options.header_text}\n"
      output << "The reversed text will follow\n"
   end

   # Implements ReverseController's :reversed_body hook
   # but can be used by any controller
   def build_reversed_body
      output << data.reverse << "\n"
   end

end

puts ReverseController.render_txt(:data => "apple",
                                :header_text => "Hello Mike, Hello Joe!")

-----
OUTPUT:

Hello Mike, Hello Joe!
The reversed text will follow
elppa

Direct Known Subclasses

CSV, HTML, MarkDown, PrawnPDF, Text

Defined Under Namespace

Modules: RenderingTools Classes: CSV, HTML, MarkDown, PrawnPDF, Template, TemplateNotDefined, Text

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RenderingTools

#render_group, #render_grouping, #render_inline_grouping, #render_row, #render_table

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ Object

Provides a shortcut for per-format handlers.

Example:

# will only be called if formatter is called for html output
html { output << "Look, I'm handling html" }


239
240
241
242
243
244
245
# File 'lib/ruport/formatter.rb', line 239

def method_missing(id,*args)
  if self.class.formats.include?(id)
    yield() if format == id
  else
    super
  end
end

Instance Attribute Details

#dataObject

Set by the :data attribute from Controller#render



130
131
132
# File 'lib/ruport/formatter.rb', line 130

def data
  @data
end

#formatObject

Set automatically by Controller#render(format) or Controller#render_format



133
134
135
# File 'lib/ruport/formatter.rb', line 133

def format
  @format
end

#optionsObject

Provides a Controller::Options object for storing formatting options.



191
192
193
# File 'lib/ruport/formatter.rb', line 191

def options
  @options ||= Controller::Options.new
end

Class Method Details

.build(stage, &block) ⇒ Object

Allows you to implement stages in your formatter using the following syntax:

class ReversedText < Ruport::Formatter
   renders :txt, :for => ReverseController

   build :reversed_header do
      output << "#{options.header_text}\n"
      output << "The reversed text will follow\n"
   end

   build :reversed_body do
      output << data.reverse << "\n"
   end
end


170
171
172
# File 'lib/ruport/formatter.rb', line 170

def self.build(stage,&block)
  define_method "build_#{stage}", &block
end

.formatsObject

Gives a list of formats registered for this formatter.



175
176
177
# File 'lib/ruport/formatter.rb', line 175

def self.formats
  @formats ||= []
end

.renders(fmts, options = {}) ⇒ Object

Registers the formatter with one or more Controllers.

renders :pdf, :for => MyController
render :text, :for => [MyController,YourController]
renders [:csv,:html], :for => YourController


145
146
147
148
149
150
151
152
# File 'lib/ruport/formatter.rb', line 145

def self.renders(fmts,options={})
  Array(fmts).each do |format|
    Array(options[:for]).each do |o|
      o.send(:add_format,self,format)
      formats << format unless formats.include?(format)
    end
  end
end

.save_as_binary_fileObject

Use to define that your formatter should save in binary format



213
214
215
216
217
# File 'lib/ruport/formatter.rb', line 213

def self.save_as_binary_file
  define_method :save_output do |filename|
    File.open(filename,"wb") {|f| f << output }
  end
end

Instance Method Details

#clear_outputObject

Clears the output.



203
204
205
# File 'lib/ruport/formatter.rb', line 203

def clear_output
  @output.replace("")
end

#erb(string, options = {}) ⇒ Object

Evaluates the string using ERB and returns the results.

If :binding is specified, it will evaluate the template in that context.



223
224
225
226
227
228
229
230
# File 'lib/ruport/formatter.rb', line 223

def erb(string,options={})
  require "erb"
  if string =~ /(\.r\w+)|(\.erb)$/
    ERB.new(File.read(string)).result(options[:binding]||binding)
  else
    ERB.new(string).result(options[:binding]||binding)
  end
end

#outputObject

Stores a string used for outputting formatted data.



185
186
187
188
# File 'lib/ruport/formatter.rb', line 185

def output
  return options.io if options.io
  @output ||= ""
end

#save_output(filename) ⇒ Object

Saves the output to a file.



208
209
210
# File 'lib/ruport/formatter.rb', line 208

def save_output(filename)
  File.open(filename,"w") {|f| f << output }
end

#templateObject

Returns the template currently set for this formatter.



180
181
182
# File 'lib/ruport/formatter.rb', line 180

def template
  Template[options.template] rescue nil || Template[:default]
end