Class: Ruport::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/ruport/format.rb,
lib/ruport/format/builder.rb,
lib/ruport/format/document.rb,
lib/ruport/format/open_node.rb

Overview

Ruport’s Format model is meant to help get your data in a suitable format for output. Rather than make too many assumptions about how you will want your data to look, a number of tools have been built so that you can quickly define those things yourself.

There are three main sets of functionality the Ruport::Format model provides.

* Structured printable document support ( Format::Document and friends)
* Text filter support ( Report#render and the Format class)
* Support for DataSet Formatting ( Format::Builder)

The support for structured printable documents is currently geared towards PDF support and needs some additional work to be truly useful. Suggestions would be much appreciated.

Format::Builder lets you define functions that will be used via DataSet#as This is primary geared towards tabular data output, but there is no reason why DataSet#as and the render_foo methods of Format::Builder cannot be adapted to fit whatever needs you may need.

The filters implemented in the Format class are meant to process strings or entire templates. The Format class will soon automatically build a Ruport::Parser for any string input. By default, filters are provided to process erb, pure ruby, and redcloth. It is trivial to extend this functionality though.

This is best shown by a simple example:

a = Ruport::Report.new
Ruport::Format.register_filter :reverser do
  content.reverse
end
a.render "somestring", :filters => [:reverser]

Output: "gnirtsemos"

Filters can be combined, and you can run them in different orders to obtain different results.

See the source for the built in filters for ideas.

Also, see Report#render for how to bind Format objects to your own classes.

When combined, filters, data set output templates, and structured printable document facilities create a complete Formatting system.

This part of Ruport is under active development. Please do feel free to submit feature requests or suggestions.

Defined Under Namespace

Classes: Builder, Document, Element, OpenNode, Page, Section

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass_binding) ⇒ Format

To hook up a Format object to your current class, you need to pass it a binding. This way, when filters are being processed, they will be evaluated in the context of the object they are being called from, rather than within an instance of Format.



71
72
73
# File 'lib/ruport/format.rb', line 71

def initialize(klass_binding)
  @binding = klass_binding
end

Instance Attribute Details

#bindingObject

This is the binding to the object Format is tied to



79
80
81
# File 'lib/ruport/format.rb', line 79

def binding
  @binding
end

#contentObject

This is the text to be processed by the filters



76
77
78
# File 'lib/ruport/format.rb', line 76

def content
  @content
end

Class Method Details

.register_filter(name, &filter_proc) ⇒ Object

Takes a name and a block and creates a filter method This will define methods in the form of Format#filter_my_filter_name.

Example:

Format.register_filter :no_ohz do
  content.gsub(/O/i,"")
end


110
111
112
# File 'lib/ruport/format.rb', line 110

def Format.register_filter(name,&filter_proc)
  define_method "filter_#{name}".to_sym, &filter_proc
end

Instance Method Details

#filter_erbObject

Processes the ERB text in @content in the context of the object that Format is bound to.



83
84
85
# File 'lib/ruport/format.rb', line 83

def filter_erb  
  ERB.new(@content).result(@binding)
end

#filter_red_clothObject

Processes the RedCloth text in @content in the context of the object that Format is bound to.



89
90
91
# File 'lib/ruport/format.rb', line 89

def filter_red_cloth
  RedCloth.new(@content).to_html
end

#filter_rubyObject

Processes the ruby code in @content in the context of the object that Format is bound to.

(Does an eval on the binding)



97
98
99
# File 'lib/ruport/format.rb', line 97

def filter_ruby
  eval(@content,@binding)
end