Class: ODFReport::Report

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

Instance Method Summary collapse

Constructor Details

#initialize(template_name = nil, io: nil) {|_self| ... } ⇒ Report

Returns a new instance of Report.

Yields:

  • (_self)

Yield Parameters:



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

def initialize(template_name = nil, io: nil)

  @template = ODFReport::Template.new(template_name, io: io)

  @texts = []
  @fields = []
  @tables = []
  @sections = []

  @images      = []

  yield(self) if block_given?

end

Instance Method Details

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



20
21
22
23
24
# File 'lib/odf-report/report.rb', line 20

def add_field(field_tag, value='')
  opts = {:name => field_tag, :value => value}
  field = Field.new(opts)
  @fields << field
end

#add_image(image_name, value = nil) ⇒ Object



48
49
50
51
52
# File 'lib/odf-report/report.rb', line 48

def add_image(image_name, value=nil)
  opts = {:name => image_name, :value => value}
  image = Image.new(opts)
  @images << image
end

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

Yields:

  • (sec)


40
41
42
43
44
45
46
# File 'lib/odf-report/report.rb', line 40

def add_section(section_name, collection, opts={})
  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)


32
33
34
35
36
37
38
# File 'lib/odf-report/report.rb', line 32

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

  yield(tab)
end

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



26
27
28
29
30
# File 'lib/odf-report/report.rb', line 26

def add_text(field_tag, value='')
  opts = {:name => field_tag, :value => value}
  text = Text.new(opts)
  @texts << text
end

#all_imagesObject



86
87
88
# File 'lib/odf-report/report.rb', line 86

def all_images
  @all_images ||= (@images.map(&:files) + @sections.map(&:all_images) + @tables.map(&:all_images)).flatten.uniq
end

#generate(dest = nil) ⇒ Object



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

def generate(dest = nil)

  @template.update_content do |file|

    file.update_files do |doc|

      @sections.each { |c| c.replace!(doc) }
      @tables.each   { |c| c.replace!(doc) }

      @texts.each    { |c| c.replace!(doc) }
      @fields.each   { |c| c.replace!(doc) }

      @images.each   { |c| c.replace!(doc) }

    end

    all_images.each { |i| Image.include_image_file(file, i) }

    file.update_manifest do |content|
      all_images.each { |i| Image.include_manifest_entry(content, i) }
    end

  end

  if dest
    File.open(dest, "wb") { |f| f.write(@template.data) }
  else
    @template.data
  end

end