Class: Prawnto::TemplateHandlers::Renderer

Inherits:
Object
  • Object
show all
Includes:
Partials
Defined in:
lib/prawnto/template_handlers/renderer.rb

Instance Method Summary collapse

Methods included from Partials

#partial!

Constructor Details

#initialize(view_context, calling_object = nil) ⇒ Renderer

Returns a new instance of Renderer.



8
9
10
11
12
13
# File 'lib/prawnto/template_handlers/renderer.rb', line 8

def initialize(view_context, calling_object = nil)
  @view_context = view_context
  @calling_object = calling_object
  set_instance_variables
  @pdf = Prawn::Document.new(@prawnto_options[:prawn]);
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object (private)

This method is a little hacky with pushing the instance variables back. I would prefer to use bindings, but wasn’t having much luck.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/prawnto/template_handlers/renderer.rb', line 48

def method_missing(m, *args, &block)
  begin
    super
  rescue
    if pdf.respond_to?(m.to_s)
      pdf.send(m, *args, &block)
    elsif @calling_object.respond_to?(m.to_s)
      push_instance_variables_to @calling_object
      res = @calling_object.send(m, *args, &block)
      copy_instance_variables_from @calling_object
      res
    elsif @calling_object != @view_context and @view_context.respond_to?(m.to_s)
      push_instance_variables_to @view_context
      res = @view_context.send(m, *args, &block)
      copy_instance_variables_from @view_context
      res
    else
      raise
    end
  end
end

Instance Method Details

#to_pdf(scope = false, &block) ⇒ Object



15
16
17
18
19
# File 'lib/prawnto/template_handlers/renderer.rb', line 15

def to_pdf(scope = false, &block)
  @_scope = scope
  instance_eval(&block)
  @pdf.render.html_safe
end