Class: RtfGenerator

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/reports/rtf_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report_generator) ⇒ RtfGenerator

Returns a new instance of RtfGenerator.



7
8
9
10
11
12
# File 'backend/app/lib/reports/rtf_generator.rb', line 7

def initialize(report_generator)
  @generator = report_generator
  @report = report_generator.report
  @content = report.get_content
  @rtf = RRTF::Document.new
end

Instance Attribute Details

#contentObject

Returns the value of attribute content



5
6
7
# File 'backend/app/lib/reports/rtf_generator.rb', line 5

def content
  @content
end

#generatorObject

Returns the value of attribute generator



5
6
7
# File 'backend/app/lib/reports/rtf_generator.rb', line 5

def generator
  @generator
end

#reportObject

Returns the value of attribute report



5
6
7
# File 'backend/app/lib/reports/rtf_generator.rb', line 5

def report
  @report
end

#rtfObject

Returns the value of attribute rtf



5
6
7
# File 'backend/app/lib/reports/rtf_generator.rb', line 5

def rtf
  @rtf
end

Instance Method Details

#content_attributes(p, hash, indent = 0) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'backend/app/lib/reports/rtf_generator.rb', line 52

def content_attributes(p, hash, indent = 0)
  p.line_break
  hash.each do |key, value|
    next unless value
    p.line_break if value.is_a?(Array)
    p << "\t" * indent
    p.apply(
      'bold' => true
    ) << "#{t(key)}: " unless key.to_s[0] == '_'
    if value.is_a?(Array)
      p.line_break
      generator.rtf_subreport(value) do |value|
        value.each do |item|
          content_attributes(p, item, indent + 1)
        end
      end
    elsif value
      p << value.to_s
      p.line_break
    end
  end
end

#content_item(item) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'backend/app/lib/reports/rtf_generator.rb', line 42

def content_item(item)
  rtf.paragraph do |p|
    p.apply(
      'bold' => true,
      'font_size' => 48
    ) << generator.identifier(item)
    content_attributes(p, item)
  end
end

#generateObject



14
15
16
17
18
19
20
21
22
# File 'backend/app/lib/reports/rtf_generator.rb', line 14

def generate
  header
  rtf.page_break unless report.page_break
  content.each do |item|
    rtf.page_break if report.page_break
    content_item(item)
  end
  rtf
end

#headerObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'backend/app/lib/reports/rtf_generator.rb', line 24

def header
  rtf.paragraph do |p|
    p.apply(
      'bold' => true,
      'font_size' => 72
    ) << report.title
    p.line_break
    report.info.each do |key, value|
      p.apply(
        'bold' => true
      ) << "#{t(key)}: " unless key.to_s[0] == '_'
      p << value.to_s
      p.line_break
    end
    p << "Report Generated at #{DateTime.now.strftime("%Y-%m-%d %H:%M %Z")}"
  end
end

#t(key) ⇒ Object



75
76
77
# File 'backend/app/lib/reports/rtf_generator.rb', line 75

def t(key)
  @generator.t(key)
end