Parent

Ruport::Formatter

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 Renderer 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 Renderer 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 ReverseRenderer < Ruport::Renderer
   stage :reversed_header, :reversed_body 
   option :header_text
end

class ReversedText < Ruport::Formatter 

   # Hooks formatter up to renderer
   renders :txt, :for => ReverseRenderer      

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

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

end    

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

-----
OUTPUT: 

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

Public Class Methods

formats() click to toggle source

Gives a list of formats registered for this formatter.

# File lib/ruport/formatter.rb, line 175
def self.formats
  @formats ||= []
end
opt_reader(*opts) click to toggle source

Allows the options specified to be accessed directly.

opt_reader :something
something == options.something #=> true
# File lib/ruport/formatter.rb, line 168
def self.opt_reader(*opts) 
  require "forwardable"
  extend Forwardable
  opts.each { |o| def_delegator :@options, o }
end
renders(fmts,options={}) click to toggle source

Registers the formatter with one or more Renderers.

renders :pdf, :for => MyRenderer
render :text, :for => [MyRenderer,YourRenderer]
renders [:csv,:html], :for => YourRenderer
# File lib/ruport/formatter.rb, line 155
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

Public Instance Methods

clear_output() click to toggle source

Clears the output.

# File lib/ruport/formatter.rb, line 196
def clear_output
  @output.replace("")
end
erb(string,options={}) click to toggle source

Evaluates the string using ERB and returns the results.

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

# File lib/ruport/formatter.rb, line 209
def erb(string,options={})      
  require "erb"
  if string =~ /\.r\w+$/
    ERB.new(File.read(string)).result(options[:binding]||binding)
  else
    ERB.new(string).result(options[:binding]||binding)
  end
end
method_missing(id,*args) click to toggle source

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" }
# File lib/ruport/formatter.rb, line 225
def method_missing(id,*args)
  if self.class.formats.include?(id)
    yield() if format == id
  else
    super
  end
end
options() click to toggle source

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

# File lib/ruport/formatter.rb, line 191
def options
  @options ||= Renderer::Options.new
end
output() click to toggle source

Stores a string used for outputting formatted data.

# File lib/ruport/formatter.rb, line 185
def output
  return options.io if options.io
  @output ||= ""
end
save_output(filename) click to toggle source

Saves the output to a file.

# File lib/ruport/formatter.rb, line 201
def save_output(filename)
  File.open(filename,"w") {|f| f << output }
end
template() click to toggle source

Returns the template currently set for this formatter.

# File lib/ruport/formatter.rb, line 180
def template
  Template[options.template]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.