Sha256: 6e4006ad240d988de94e887917dadcfc0466376ee4e0515406cd5205cde8a39f

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

# The recommended way to extend Prawn's functionality is to include the
# <code>Prawn::View</code> mixin in your own class, which will make all
# <code>Prawn::Document</code> methods available to your custom objects.
#
# This approach is preferred over inheriting from
# <code>Prawn::Document</code>, as your state will be kept completely separate
# from <code>Prawn::Document</code>'s, thus avoiding accidental method
# collisions.
#
# Note that <code>Prawn::View</code> lazily instantiates a
# <code>Prawn::Document</code> with default initialization settings, such as
# page size, layout, margins, etc.
#
# By defining your own <code>document</code> method, as shown in the example,
# you will be able to override those settings and initialize a
# <code>Prawn::Document</code> to your heart's content. This method will be
# called repeatedly by <code>Prawn::View</code>, so be sure to memoize the
# object by assigning it to an instance variable via the <code>||=</code>
# operator.

require_relative '../example_helper'

class Greeter
  include Prawn::View

  def initialize(name)
    @name = name
  end

  def say_hello
    text "Hello, #{@name}!"
  end

  def say_goodbye
    font('Courier') do
      text "Goodbye, #{@name}!"
    end
  end
end

greeter = Greeter.new('Gregory')

greeter.say_hello
greeter.say_goodbye

greeter.save_as('greetings.pdf')

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prawn-2.4.0 manual/basic_concepts/view.rb
prawn-2.3.0 manual/basic_concepts/view.rb