Class: ODFReport::Report

Inherits:
Object
  • Object
show all
Includes:
FileOps, HashGsub, Images
Defined in:
lib/odf-report/report.rb

Constant Summary

Constants included from HashGsub

HashGsub::HTML_ESCAPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashGsub

#hash_gsub!, #html_escape, #node_hash_gsub!, #odf_linebreak

Methods included from FileOps

#add_dir_to_zip, #add_files_to_dir, #create_new_file, #random_filename, #update_file_from_zip

Methods included from Images

#find_image_name_matches, #replace_images

Constructor Details

#initialize(template_name) {|_self| ... } ⇒ Report

Returns a new instance of Report.

Yields:

  • (_self)

Yield Parameters:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/odf-report/report.rb', line 8

def initialize(template_name, &block)
  @template = template_name

  @values = {}
  @tables = []
  @images = {}
  @image_names_replacements = {}
  @sections = []

  @tmp_dir = File.join(Dir.tmpdir, random_filename(:prefix=>'odt_'))
  Dir.mkdir(@tmp_dir) unless File.exists? @tmp_dir

  yield(self)

end

Instance Attribute Details

#imagesObject

Returns the value of attribute images.



6
7
8
# File 'lib/odf-report/report.rb', line 6

def images
  @images
end

#sectionsObject

Returns the value of attribute sections.



6
7
8
# File 'lib/odf-report/report.rb', line 6

def sections
  @sections
end

#tablesObject

Returns the value of attribute tables.



6
7
8
# File 'lib/odf-report/report.rb', line 6

def tables
  @tables
end

#valuesObject

Returns the value of attribute values.



6
7
8
# File 'lib/odf-report/report.rb', line 6

def values
  @values
end

Instance Method Details

#add_field(field_tag, value = '') ⇒ Object



24
25
26
# File 'lib/odf-report/report.rb', line 24

def add_field(field_tag, value='')
  @values[field_tag] = value
end

#add_image(name, path) ⇒ Object



44
45
46
# File 'lib/odf-report/report.rb', line 44

def add_image(name, path)
  @images[name] = path
end

#add_section(section_name, collection, opts = {}) {|sec| ... } ⇒ Object

Yields:

  • (sec)


36
37
38
39
40
41
42
# File 'lib/odf-report/report.rb', line 36

def add_section(section_name, collection, opts={}, &block)
  opts.merge!(:name => section_name, :collection => collection)
  sec = Section.new(opts)
  @sections << sec

  yield(sec)
end

#add_table(table_name, collection, opts = {}) {|tab| ... } ⇒ Object

Yields:

  • (tab)


28
29
30
31
32
33
34
# File 'lib/odf-report/report.rb', line 28

def add_table(table_name, collection, opts={}, &block)
  opts.merge!(:name => table_name, :collection => collection)
  tab = Table.new(opts)
  @tables << tab

  yield(tab)
end

#generate(dest = nil) ⇒ Object



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
# File 'lib/odf-report/report.rb', line 48

def generate(dest = nil)

  new_file = create_new_file(dest)

  %w(content.xml styles.xml).each do |content_file|

    update_file_from_zip(new_file, content_file) do |txt|

      parse_document(txt) do |doc|

        replace_fields!(doc)
        replace_sections!(doc)
        replace_tables!(doc)

        find_image_name_matches(doc)

      end

    end

  end

  replace_images(new_file)

  new_file

end