Class: ReportBuilder::Builder

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

Html, Pdf, Rtf, Text

Defined Under Namespace

Classes: Html, Pdf, Rtf, Text

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options)
  @builder=builder
  @parse_level=0
  @options=default_options.merge(options)
  @toc=[]
  @table_n=1
  @graph_n=1
  @entry_n=1
  @list_tables=[]
  @list_graphs=[]

end

Instance Attribute Details

#optionsObject (readonly)

Options for Builder. Passed by ReportBuilder class on creation



10
11
12
# File 'lib/reportbuilder/builder.rb', line 10

def options
  @options
end

#parse_levelObject (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

#tocObject (readonly)

Entries for Table of Contents



12
13
14
# File 'lib/reportbuilder/builder.rb', line 12

def toc
  @toc
end

Class Method Details

.codeObject

Array of string with format names for the Builder. For example, Html builder returns %whtm and Text builde returns %wtxt



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_classesObject

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_optionsObject

:nodoc:



48
49
50
# File 'lib/reportbuilder/builder.rb', line 48

def default_options # :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

#parseObject

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