Class: ThousandIsland::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/thousand_island/builder.rb

Overview

Your Builder class is where you will put the necessary logic for rendering the final pdf. It’s up to you how you get the data into the Builder. It will depend on the complexity. You might just pass an Invoice object (MyBuilder.new(invoice))) or you may have a bunch of methods that are called by an external object to get the data where it needs to be.

You must declare which Template class you will be using. Failing to do so will raise a TemplateRequiredError when you call the build method. Declare the template with the following in the main class body:

uses_template MyTemplate

Your Builder can have a filename method, which will help a Rails Controller or other class determine the name to use to send the file to the browser or save to the filesystem (or both). Without this method it will have a default name, so you may choose to put the naming logic for your file elsewhere, it’s up to you.

You must have a body_content method that takes no arguments (or the pdf will be empty!). This is the method that is passed around internally in order for Prawn to render what is in the method. You can use raw Prawn syntax, or any of the style magic methods to render to the pdf. You may also call other methods from your body_content method, and use Prawn syntax and magic methods in those too.

A Builder example might be:

 class MyBuilder < ThousandIsland::Builder
   uses_template MyTemplate

   attr_reader :data

   def initialize(data={})
     @data = data
     # do something with the data...
   end

   def filename
     "Document#{data.id_number}"
   end

   def body_content
     # call custom methods, magic methods or call Prawn methods directly:
     h1 'Main Heading'
     display_info
     body 'Main text in here...'
   end

   # Custom method called by body_content
   def display_info
     body "Written by: #{data.author}"
     pdf.image data.avatar, height: 20
   end
 end

Finally, to get the finished pdf from your Builder, call the build method like so:

pdf = my_builder.build

Optional:

Define a header_content method to add content below whatever is defined in the Template. This will be repeated according to the header settings in the Template.

Define a footer_content method to add content above whatever is defined in the Template. This will be repeated according to the footer settings in the Template.

Define a settings method that returns a Hash. This will be passed to the Template class and will override any of the Template default settings.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ Builder

Returns a new instance of Builder.



90
91
# File 'lib/thousand_island/builder.rb', line 90

def initialize(data=nil)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object (private)

Respond to methods that relate to the style_sheet known styles



144
145
146
147
148
149
150
# File 'lib/thousand_island/builder.rb', line 144

def method_missing(method_name, *arguments, &block)
  if template.available_styles.include?(method_name)
    template.send(method_name, *arguments)
  else
    super
  end
end

Class Attribute Details

.template_klassObject



80
81
82
83
# File 'lib/thousand_island/builder.rb', line 80

def template_klass
  raise TemplateRequiredError.new 'Builders must set a Template class with #uses_template in the Class body' if @template_klass.nil?
  @template_klass
end

Instance Attribute Details

#pdfObject (readonly)

Returns the value of attribute pdf.



75
76
77
# File 'lib/thousand_island/builder.rb', line 75

def pdf
  @pdf
end

Class Method Details

.uses_template(klass) ⇒ Object



85
86
87
# File 'lib/thousand_island/builder.rb', line 85

def uses_template(klass)
  self.template_klass = klass
end

Instance Method Details

#buildObject

Returns the finished pdf ready to save to the filesystem or send back to the user’s browser



98
99
100
101
102
103
104
# File 'lib/thousand_island/builder.rb', line 98

def build
  #body first so we know how many pages for repeated components
  draw_body
  draw_header
  draw_footer
  pdf.render
end

#filenameObject



93
94
95
# File 'lib/thousand_island/builder.rb', line 93

def filename
  'default_filename'
end

#settingsObject



106
107
108
# File 'lib/thousand_island/builder.rb', line 106

def settings
  {}
end

#table_with(klass) ⇒ Object



110
111
112
# File 'lib/thousand_island/builder.rb', line 110

def table_with(klass)
  klass.new(pdf)
end