This module provides hooks into Ruport's formatting system. It is used to implement the as() method for all of Ruport's data structures, as well as the renders_with and renders_as_* helpers.
You can actually use this with any data structure, it will look for a renderable_data(format) method to pass to the controller you specify, but if that is not defined, it will pass self.
Examples:
# Render Arrays with Ruport's Row Controller
class Array
include Ruport::Controller::Hooks
renders_as_row
end
# >> [1,2,3].as(:csv)
# => "1,2,3\n"
# Render Hashes with Ruport's Row Controller
class Hash
include Ruport::Controller::Hooks
renders_as_row
attr_accessor :column_order
def renderable_data(format)
column_order.map { |c| self[c] }
end
end
# >> a = { :a => 1, :b => 2, :c => 3 }
# >> a.column_order = [:b,:a,:c]
# >> a.as(:csv)
# => "2,1,3\n"
Uses the Controller specified by renders_with to generate formatted output. Passes the return value of the renderable_data(format) method if the method is defined, otherwise passes self as :data
The remaining options are converted to a Controller::Options object and are accessible in both the controller and formatter.
Example: table.as(:csv, :show_table_headers => false)
# File lib/ruport/controller.rb, line 168 def as(format,options={}) raise ControllerNotSetError unless self.class.controller unless self.class.controller.formats.include?(format) raise UnknownFormatError end self.class.controller.render(format, self.class.rendering_options.merge(options)) do |rend| rend.data = respond_to?(:renderable_data) ? renderable_data(format) : self yield(rend) if block_given? end end
Generated with the Darkfish Rdoc Generator 2.