| Class | Ruport::Controller |
| In: |
lib/ruport/controller.rb
|
| Parent: | Object |
This class implements the core controller for Ruport’s formatting system. It is designed to implement the low level tools necessary to build report controllers for different kinds of tasks. See Controller::Table for a tabular data controller.
| format | [RW] | The name of format being used. |
| formatter | [W] | The formatter object being used. |
Returns a hash that maps format names to their formatter classes, for use with the formatter shortcut. Supported formats are :html, :csv, :pdf, and :text by default.
Sample override:
class MyController < Ruport::Controller
def built_in_formats
super.extend(:xml => MyXMLFormatter,
:json => MyJSONFormatter)
end
end
This would allow for:
class ChildController < MyController
formatter :xml do
# ...
end
formatter :json do
# ...
end
end
Registers a hook to look for in the Formatter object when the render() method is called.
Usage:
class MyController < Ruport::Controller
# other details omitted...
finalize :apple
end
class MyFormatter < Ruport::Formatter
renders :example, :for => MyController
# other details omitted...
def finalize_apple
# this method will be called when MyController tries to render
# the :example format
end
end
If a formatter does not implement this hook, it is simply ignored.
Lists the formatters that are currently registered on a controller, as a hash keyed by format name.
Example:
>> Ruport::Controller::Table.formats
=> {:html=>Ruport::Formatter::HTML,
?> :csv=>Ruport::Formatter::CSV,
?> :text=>Ruport::Formatter::Text,
?> :pdf=>Ruport::Formatter::PDF}
Generates an anonymous formatter class and ties it to the Controller. This method looks up the built in formats in the hash returned by built_in_formats, but also explicitly specify a custom Formatter class to subclass from.
Sample usage:
class ControllerWithAnonymousFormatters < Ruport::Controller
stage :report
formatter :html do
build :report do
output << textile("h1. Hi there")
end
end
formatter :csv do
build :report do
build_row([1,2,3])
end
end
formatter :pdf do
build :report do
add_text "hello world"
end
end
formatter :text do
build :report do
output << "Hello world"
end
end
formatter :custom => CustomFormatter do
build :report do
output << "This is "
custom_helper
end
end
end
Registers a hook to look for in the Formatter object when the render() method is called.
Usage:
class MyController < Ruport::Controller
# other details omitted...
prepare :apple
end
class MyFormatter < Ruport::Formatter
renders :example, :for => MyController
def prepare_apple
# this method will be called when MyController tries to render
# the :example format
end
# other details omitted...
end
If a formatter does not implement this hook, it is simply ignored.
Builds up a controller object, looks up the appropriate formatter, sets the data and options, and then does the following process:
* If the controller contains a module Helpers, mix it in to the instance. * If a block is given, yield the Controller instance. * If a setup() method is defined on the Controller, call it. * Call the run() method. * If the :file option is set to a file name, appends output to the file. * Return the results of formatter.output
Please see the examples/ directory for custom controller examples, because this is not nearly as complicated as it sounds in most cases.
Defines attribute writers for the Controller::Options object shared between Controller and Formatter. Will throw an error if the user does not provide values for these options upon rendering.
usage:
class MyController < Ruport::Controller
required_option :employee_name, :address
# other details omitted
end
Registers hooks to look for in the Formatter object when the render() method is called.
Usage:
class MyController < Ruport::Controller
# other details omitted...
stage :apple,:banana
end
class MyFormatter < Ruport::Formatter
renders :example, :for => MyController
def build_apple
# this method will be called when MyController tries to render
# the :example format
end
def build_banana
# this method will be called when MyController tries to render
# the :example format
end
# other details omitted...
end
If a formatter does not implement these hooks, they are simply ignored.
If an IO object is given, Formatter#output will use it instead of the default String. For Ruport’s core controllers, we technically can use any object that supports the << method, but it’s meant for IO objects such as File or STDOUT
Controller::Options object which is shared with the current formatter.