Class: Raport::Report::Renderer

Inherits:
Object
  • Object
show all
Defined in:
app/services/raport/report/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report) ⇒ Renderer

Returns a new instance of Renderer.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/services/raport/report/renderer.rb', line 9

def initialize(report)
  @report = report
  @view ||= begin
    view = ActionView::Base.new
    view.view_paths = ActionController::Base.view_paths
    view.extend ApplicationHelper
    view
  end
  @tmpfile ||= begin
    tmpfile = Tempfile.new(report.tmp_filename)
    tmpfile.write(render_view)
    tmpfile
  end
end

Instance Attribute Details

#reportObject

Returns the value of attribute report.



5
6
7
# File 'app/services/raport/report/renderer.rb', line 5

def report
  @report
end

#tmpfileObject (readonly)

Returns the value of attribute tmpfile.



7
8
9
# File 'app/services/raport/report/renderer.rb', line 7

def tmpfile
  @tmpfile
end

#viewObject (readonly)

Returns the value of attribute view.



7
8
9
# File 'app/services/raport/report/renderer.rb', line 7

def view
  @view
end

Instance Method Details

#renderObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/raport/report/renderer.rb', line 24

def render
  begin
    report.activate
    report.file = tmpfile
    report.finish
  rescue => e
    report.last_error = [e.message, e.backtrace.join("\n")].join("\n")
    report.flop
    raise
  ensure
    tmpfile.close

    case tmpfile.class.name
      when 'Tempfile' then tmpfile.unlink
      when 'File' then File.unlink(tmpfile.path)
    end
  end
end