Class: ODFReport

Inherits:
Object
  • Object
show all
Defined in:
lib/odf-report.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ODFReport.

Yields:

  • (_self)

Yield Parameters:

  • _self (ODFReport)

    the object that the method was called on



7
8
9
10
11
12
13
14
15
# File 'lib/odf-report.rb', line 7

def initialize(template_name, &block)
  @template = template_name
  @data={:values=>{}, :tables=>{}, :images => {} }

  @tmp_dir = Dir.tmpdir + "/" + random_filename(:prefix=>'odt_')
  Dir.mkdir(@tmp_dir) unless File.exists? @tmp_dir

  yield self
end

Instance Method Details

#add_field(field_tag, value) ⇒ Object



17
18
19
# File 'lib/odf-report.rb', line 17

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

#add_image(name, path) ⇒ Object



33
34
35
# File 'lib/odf-report.rb', line 33

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

#add_table(table_tag, collection, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/odf-report.rb', line 21

def add_table(table_tag, collection, &block)

  @data[:tables][table_tag] = []

  collection.each do |item|
    row = {}
    yield(row, item)
    @data[:tables][table_tag] << row
  end

end

#generate(dest = nil) ⇒ Object



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

def generate(dest = nil)

  if dest

    FileUtils.cp(@template, dest)
    new_file = dest

  else

    FileUtils.cp(@template, @tmp_dir)
    new_file = "#{@tmp_dir}/#{File.basename(@template)}"

  end

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

    update_file_from_zip(new_file, content_file) do |txt|

      replace_fields!(txt)
      replace_tables!(txt)
      replace_image_refs!(txt)
    end

  end

  unless @data[:images].empty?
    image_dir_name = "Pictures"
    dir = File.join("#{@tmp_dir}", image_dir_name)
    add_image_files_to_dir(dir)
    add_dir_to_zip(new_file, dir, image_dir_name)
  end

  new_file

end