Class: Phlex::PDF

Inherits:
Object
  • Object
show all
Includes:
Prawn::View
Defined in:
lib/phlex/pdf.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.document(document = Prawn::Document.new) ⇒ Object



79
80
81
82
# File 'lib/phlex/pdf.rb', line 79

def document(document = Prawn::Document.new)
  new.call(document)
  document
end

.renderObject



84
85
86
# File 'lib/phlex/pdf.rb', line 84

def render(...)
  document.render(...)
end

.render_fileObject



88
89
90
# File 'lib/phlex/pdf.rb', line 88

def render_file(...)
  document.render_file(...)
end

Instance Method Details

#after_templatenil

This method is abstract.

Override this method to hook in right after a template is rendered. Please remember to call super so that callbacks can be added at different layers of the inheritance tree.

Returns:

  • (nil)


43
44
45
# File 'lib/phlex/pdf.rb', line 43

def after_template
  nil
end

#around_templatenil

This method is abstract.

Override this method to hook in around a template render. You can do things before and after calling super to render the template. You should always call super so that callbacks can be added at different layers of the inheritance tree.

Returns:

  • (nil)


28
29
30
31
32
33
# File 'lib/phlex/pdf.rb', line 28

def around_template
  before_template
  yield
  after_template
  nil
end

#before_templatenil

This method is abstract.

Override this method to hook in right before a template is rendered. Please remember to call super so that callbacks can be added at different layers of the inheritance tree.

Returns:

  • (nil)


37
38
39
# File 'lib/phlex/pdf.rb', line 37

def before_template
  nil
end

#call(document, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/phlex/pdf.rb', line 13

def call(document, &block)
  @document = document
  around_template do
    if block_given?
      view_template do
        yield_content(&block)
      end
    else
      view_template
    end
  end
end

#render(renderable, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/phlex/pdf.rb', line 59

def render(renderable, &block)
  case renderable
  when Phlex::PDF
    renderable.call(@document, &block)
  when String
    @document.text renderable
  when Class
    render(renderable.new, &block) if renderable < Phlex::PDF
  when Proc, Method
    yield_content(&renderable)
  when Enumerable
    renderable.each { |r| render(r, &block) }
  else
    raise ArgumentError, "You can't render a #{renderable.inspect}."
  end

  nil
end

#yield_content(&block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/phlex/pdf.rb', line 47

def yield_content(&block)
  return unless block_given?

  if block.arity.zero?
    # This handles lambdas and ->
    yield
  else
    # This handles Proc and proc
    yield self
  end
end