Class: Ruport::Formatter::Template

Inherits:
Controller::Options show all
Defined in:
lib/ruport/formatter/template.rb

Overview

This class provides templating functionality for Ruport. New templates are created using the Template.create method.

Example:

Ruport::Formatter::Template.create(:simple) do |t|
  t.page_layout = :landscape
  t.grouping_style = :offset
end

You can then determine how the template should be used by defining an apply_template method in your formatter.

Example:

class Ruport::Formatter::PDF 
  def apply_template
    options.paper_orientation = template.page_layout
    options.style = template.grouping_style
  end
end

When you're ready to render the output, you can set the :template as an option for the formatter. Using the template remains optional and you can still render the report without it.

Example:

puts g.to_pdf(:template => :simple) #=> uses the template
puts g.to_pdf                       #=> doesn't use the template

The built-in formatters all have apply_template methods defined that accept a standard set of options. Each option can be set by supplying a hash with the keys/values listed in the tables below.

Example:

Ruport::Formatter::Template.create(:simple) do |format|
  format.page = {
    :size   => "LETTER",
    :layout => :landscape
  }
end

If you define a template with the name :default, then it will be used by all formatters unless they either specify a template or explicitly turn off the templating functionality by using :template => false.

Example:

Ruport::Formatter::Template.create(:simple)
Ruport::Formatter::Template.create(:default)

puts g.to_pdf                       #=> uses the :default template
puts g.to_pdf(:template => :simple) #=> uses the :simple template
puts g.to_pdf(:template => false)   #=> doesn't use a template

PDF Formatter Options

Option          Key                 Value

page            :size               Any size supported by the :paper
                                    option to PDF::Writer.new

                :layout             :portrait, :landscape

text            Any available to    Corresponding values
                PDF::Writer#text

table           All attributes of   Corresponding values
                PDF::SimpleTable

                :column_options     - All attributes of
                                      PDF::SimpleTable::Column
                                      except :heading
                                    - Hash keyed by a column name, whose
                                      value is a hash containing any of
                                      the other:column_options (sets values
                                      for specific columns)
                                    - :heading => { All attributes of
                                      PDF::SimpleTable::Column::Heading }

column          :alignment          :left, :right, :center, :full

                :width              column width

heading         :alignment          :left, :right, :center, :full

                :bold               true or false

                :title              heading title (if not set,
                                    defaults to column name)

grouping        :style              :inline, :justified, :separated, :offset

Text Formatter Options

Option          Key                 Value

table           :show_headings      true or false
                :width              Table width
                :ignore_width       true or false

column          :alignment          :center
                :maximum_width      Max column width

grouping        :show_headings      true or false

HTML Formatter Options

Option          Key                 Value

table           :show_headings      true or false

grouping        :style              :inline, :justified
                :show_headings      true or false

CSV Formatter Options

Option          Key                 Value

table           :show_headings      true or false

grouping        :style              :inline, :justified, :raw
                :show_headings      true or false

format_options  All options         Corresponding values
                available to
                FasterCSV.new

Class Method Summary collapse

Methods inherited from Controller::Options

#[], #[]=, #to_hash

Class Method Details

.[](label) ⇒ Object

Returns an existing template with the provided name (label).



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

def self.[](label) 
  templates[label] or raise Ruport::Formatter::TemplateNotDefined
end

.create(label, opts = {}) {|obj| ... } ⇒ Object

Creates a new template with a name given by label.

Example:

Ruport::Formatter::Template.create(:simple) do |t|
  t.page_layout = :landscape
  t.grouping_style = :offset
end

You can inherit all the options set in a template by using the :base option and providing an existing template name to use as the base.

Example:

Ruport::Formatter::Template.create(:derived, :base => :simple)

Yields:

  • (obj)


168
169
170
171
172
173
174
175
176
# File 'lib/ruport/formatter/template.rb', line 168

def self.create(label,opts={})
  if opts[:base]
    obj = Marshal.load(Marshal.dump(self[opts[:base]]))
  else
    obj = new
  end
  yield(obj) if block_given?
  templates[label] = obj
end

.defaultObject

Returns the default template.



184
185
186
# File 'lib/ruport/formatter/template.rb', line 184

def self.default
  templates[:default]
end

.templatesObject

Returns all existing templates in a hash keyed by the template names.



148
149
150
# File 'lib/ruport/formatter/template.rb', line 148

def self.templates
  @templates ||= Hash.new 
end