Class: Prawnto::TemplateHandlers::Renderer

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

Instance Method Summary collapse

Constructor Details

#initialize(view_context, calling_object = nil) ⇒ Renderer

Returns a new instance of Renderer.



4
5
6
7
8
9
# File 'lib/prawnto/template_handlers/renderer.rb', line 4

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.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/prawnto/template_handlers/renderer.rb', line 43

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(&block) ⇒ Object



11
12
13
14
# File 'lib/prawnto/template_handlers/renderer.rb', line 11

def to_pdf(&block)
  instance_eval(&block)
  @pdf.render.html_safe
end