Class: ReportBuilder::Builder
- Inherits:
-
Object
- Object
- ReportBuilder::Builder
- Defined in:
- lib/reportbuilder/builder.rb,
lib/reportbuilder/builder/pdf.rb,
lib/reportbuilder/builder/rtf.rb,
lib/reportbuilder/builder/html.rb,
lib/reportbuilder/builder/text.rb
Overview
Abstract Builder. A builder is a class which control the output for a ReportBuilder object Every object which have a #report_building() method could be parsed with #parse_element method.
Defined Under Namespace
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Options for Builder.
-
#parse_level ⇒ Object
readonly
Level of heading.
-
#toc ⇒ Object
readonly
Entries for Table of Contents.
Class Method Summary collapse
-
.code ⇒ Object
Array of string with format names for the Builder.
- .inherited(subclass) ⇒ Object
-
.inherited_classes ⇒ Object
raise “Implement this”.
Instance Method Summary collapse
-
#default_options ⇒ Object
:nodoc:.
- #graph(opt = Hash.new, &block) ⇒ Object
-
#graph_entry(name) ⇒ Object
Add an entry for graph index.
-
#html(t) ⇒ Object
Add html code.
-
#image(img, opt = Hash.new) ⇒ Object
Create and parse an image, detecting if is a Filename or a blob.
-
#image_blob(blob, opt = Hash.new) ⇒ Object
Create and parse an image, using a string or IO.
-
#image_filename(filename, opt = Hash.new) ⇒ Object
Create and parse an image, using a string or IO.
-
#initialize(builder, options) ⇒ Builder
constructor
A new instance of Builder.
-
#parse ⇒ Object
Parse the output.
-
#parse_cycle(container) ⇒ Object
Parse each #elements of the container.
-
#parse_element(element) ⇒ Object
Parse one object, using this workflow * If is a block, evaluate it in the context of the builder * Use method #report_building_CODE, where CODE is one of the codes defined by ReportBuilder::Builder.code * Use #report_building * Use #to_s.
-
#preformatted(t) ⇒ Object
Add preformatted text.
-
#save(filename) ⇒ Object
Save the output of builder to a file.
-
#section(opt = Hash.new, &block) ⇒ Object
Create and parse an image.
-
#table(opt = Hash.new, &block) ⇒ Object
Create and parse a table.
-
#table_entry(name) ⇒ Object
Add an entry for table index.
-
#text(t) ⇒ Object
Add a paragraph to the report.
-
#toc_entry(name) ⇒ Object
Add a TOC (Table of Contents) entry Return the name of anchor.
Constructor Details
#initialize(builder, options) ⇒ Builder
Returns a new instance of Builder.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/reportbuilder/builder.rb', line 25 def initialize(builder, ) @builder=builder @parse_level=0 @options=.merge() @toc=[] @table_n=1 @graph_n=1 @entry_n=1 @list_tables=[] @list_graphs=[] end |
Instance Attribute Details
#options ⇒ Object (readonly)
Options for Builder. Passed by ReportBuilder class on creation
10 11 12 |
# File 'lib/reportbuilder/builder.rb', line 10 def @options end |
#parse_level ⇒ Object (readonly)
Level of heading. See ReportBuilder::Section for using it.
8 9 10 |
# File 'lib/reportbuilder/builder.rb', line 8 def parse_level @parse_level end |
#toc ⇒ Object (readonly)
Entries for Table of Contents
12 13 14 |
# File 'lib/reportbuilder/builder.rb', line 12 def toc @toc end |
Class Method Details
.code ⇒ Object
16 17 18 |
# File 'lib/reportbuilder/builder.rb', line 16 def self.code # raise "Implement this" end |
.inherited(subclass) ⇒ Object
22 23 24 |
# File 'lib/reportbuilder/builder.rb', line 22 def self.inherited(subclass) inherited_classes << subclass end |
.inherited_classes ⇒ Object
raise “Implement this”
19 20 21 |
# File 'lib/reportbuilder/builder.rb', line 19 def self.inherited_classes @@inherited_classes||=Array.new end |
Instance Method Details
#default_options ⇒ Object
:nodoc:
48 49 50 |
# File 'lib/reportbuilder/builder.rb', line 48 def # :nodoc: Hash.new end |
#graph(opt = Hash.new, &block) ⇒ Object
84 85 86 |
# File 'lib/reportbuilder/builder.rb', line 84 def graph(opt=Hash.new, &block) parse_element(ReportBuilder::Graph.new(opt,&block)) end |
#graph_entry(name) ⇒ Object
Add an entry for graph index. Returns the name of the anchor
141 142 143 144 145 146 |
# File 'lib/reportbuilder/builder.rb', line 141 def graph_entry(name) anchor="graph_#{@graph_n}" @graph_n+=1 @list_graphs.push([anchor,name]) anchor end |
#html(t) ⇒ Object
Add html code. Only parsed with builder which understand html
116 117 118 |
# File 'lib/reportbuilder/builder.rb', line 116 def html(t) raise "Implement this" end |
#image(img, opt = Hash.new) ⇒ Object
Create and parse an image, detecting if is a Filename or a blob
98 99 100 101 102 103 104 |
# File 'lib/reportbuilder/builder.rb', line 98 def image(img, opt=Hash.new) if img.is_a? String and File.exists? img parse_element(ReportBuilder::ImageFilename.new(img,opt)) else parse_element(ReportBuilder::ImageBlob.new(img,opt)) end end |
#image_blob(blob, opt = Hash.new) ⇒ Object
Create and parse an image, using a string or IO
88 89 90 |
# File 'lib/reportbuilder/builder.rb', line 88 def image_blob(blob, opt=Hash.new) parse_element(ReportBuilder::ImageBlob.new(blob,opt)) end |
#image_filename(filename, opt = Hash.new) ⇒ Object
Create and parse an image, using a string or IO
92 93 94 |
# File 'lib/reportbuilder/builder.rb', line 92 def image_filename(filename, opt=Hash.new) parse_element(ReportBuilder::ImageFilename.new(filename,opt)) end |
#parse ⇒ Object
Parse the output. Could be reimplemented on subclasses
38 39 40 |
# File 'lib/reportbuilder/builder.rb', line 38 def parse parse_cycle(@builder) end |
#parse_cycle(container) ⇒ Object
Parse each #elements of the container
53 54 55 56 57 58 59 |
# File 'lib/reportbuilder/builder.rb', line 53 def parse_cycle(container) @parse_level+=1 container.elements.each do |element| parse_element(element) end @parse_level-=1 end |
#parse_element(element) ⇒ Object
Parse one object, using this workflow
-
If is a block, evaluate it in the context of the builder
-
Use method #report_building_CODE, where CODE is one of the codes defined by ReportBuilder::Builder.code
-
Use #report_building
-
Use #to_s
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/reportbuilder/builder.rb', line 67 def parse_element(element) methods=self.class.code.map {|m| ("report_building_"+m).intern} if element.is_a? Proc element.arity < 1 ? instance_eval(&element) : element.call(self) elsif method=methods.find {|m| element.respond_to? m} element.send(method, self) elsif element.respond_to? :report_building element.send(:report_building, self) else text(element.to_s) end end |
#preformatted(t) ⇒ Object
Add preformatted text
120 121 122 |
# File 'lib/reportbuilder/builder.rb', line 120 def preformatted(t) raise "Implement this" end |
#save(filename) ⇒ Object
Save the output of builder to a file
42 43 44 45 46 |
# File 'lib/reportbuilder/builder.rb', line 42 def save(filename) File.open(filename, "wb") do |fp| fp.write(out) end end |
#section(opt = Hash.new, &block) ⇒ Object
Create and parse an image. Use a block to insert element inside the block
107 108 109 |
# File 'lib/reportbuilder/builder.rb', line 107 def section(opt=Hash.new, &block) parse_element(ReportBuilder::Section.new(opt,&block)) end |
#table(opt = Hash.new, &block) ⇒ Object
Create and parse a table. Use a block to control the table
81 82 83 |
# File 'lib/reportbuilder/builder.rb', line 81 def table(opt=Hash.new, &block) parse_element(ReportBuilder::Table.new(opt,&block)) end |
#table_entry(name) ⇒ Object
Add an entry for table index. Returns the name of the anchor
133 134 135 136 137 138 |
# File 'lib/reportbuilder/builder.rb', line 133 def table_entry(name) anchor="table_#{@table_n}" @table_n+=1 @list_tables.push([anchor,name]) anchor end |
#text(t) ⇒ Object
Add a paragraph to the report
112 113 114 |
# File 'lib/reportbuilder/builder.rb', line 112 def text(t) raise "Implement this" end |
#toc_entry(name) ⇒ Object
Add a TOC (Table of Contents) entry Return the name of anchor
125 126 127 128 129 130 |
# File 'lib/reportbuilder/builder.rb', line 125 def toc_entry(name) anchor="toc_#{@entry_n}" @entry_n+=1 @toc.push([anchor, name, parse_level]) anchor end |