/
report.rb
99 lines (67 loc) · 1.65 KB
/
report.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module ODFReport
class Report
include Fields, Images
attr_accessor :fields, :tables, :images, :sections, :file
def initialize(template_name, &block)
@file = ODFReport::File.new(template_name)
@fields = []
@tables = []
@images = {}
@image_names_replacements = {}
@sections = []
yield(self)
end
def add_field(field_tag, value='', &block)
opts = {:name => field_tag, :value => value}
field = Field.new(opts, &block)
@fields << field
end
def add_table(table_name, collection, opts={}, &block)
opts.merge!(:name => table_name, :collection => collection)
tab = Table.new(opts)
@tables << tab
yield(tab)
end
def add_section(section_name, collection, opts={}, &block)
opts.merge!(:name => section_name, :collection => collection)
sec = Section.new(opts)
@sections << sec
yield(sec)
end
def add_image(name, path)
@images[name] = path
end
def generate(dest = nil)
@file.create(dest)
@file.update('content.xml', 'styles.xml') do |txt|
parse_document(txt) do |doc|
replace_fields!(doc)
replace_sections!(doc)
replace_tables!(doc)
find_image_name_matches(doc)
end
end
replace_images(@file)
@file.path
end
private
def parse_document(txt)
doc = Nokogiri::XML(txt)
yield doc
txt.replace(doc.to_s)
end
def replace_fields!(content)
field_replace!(content)
end
def replace_tables!(content)
@tables.each do |table|
table.replace!(content)
end
end
def replace_sections!(content)
@sections.each do |section|
section.replace!(content)
end
end
end
end