module Prawn::Document::Snapshot

Constants

RollbackTransaction

Public Instance Methods

rollback() click to toggle source

Call this within a transaction block to roll back the transaction and prevent any of its data from being rendered. You must reset the y-position yourself if you have performed any drawing operations that modify it.

# File lib/prawn/document/snapshot.rb, line 22
def rollback
  raise RollbackTransaction
end
transaction() { || ... } click to toggle source

Run a block of drawing operations, to be completed atomically. If rollback is called or a RollbackTransaction exception is raised inside the block, all actions taken inside the block will be rolled back (with the exception of y-position, which you must restore yourself).

Returns true on success, or false if the transaction was rolled back.

# File lib/prawn/document/snapshot.rb, line 34
def transaction
  snap = take_snapshot
  yield
  true
rescue RollbackTransaction
  restore_snapshot(snap)
  false
end

Private Instance Methods

restore_snapshot(shot) click to toggle source

Rolls the page state back to the state of the given snapshot.

# File lib/prawn/document/snapshot.rb, line 59
def restore_snapshot(shot)
  # Because these objects are referenced by identifier from the Pages
  # dictionary, we can't just restore them over the current refs in
  # page_content and current_page. We have to restore them over the old
  # ones.
  page.content = shot[:page_content].identifier
  page.content.replace shot[:page_content]

  page.dictionary = shot[:current_page].identifier
  page.dictionary.replace shot[:current_page]
  page.dictionary.data[:Contents] = page.content

  @page_number = shot[:page_number]

  @page_number = shot[:page_number]

  @store.pages.data[:Kids] = shot[:page_kids].map{|id| @store[id]}
  @store.pages.data[:Count] = shot[:page_kids].size

  if shot[:dests]
    names.data[:Dests] = shot[:dests] 
  end
end
take_snapshot() click to toggle source

Takes a current snapshot of the document’s state, sufficient to reconstruct it after it was amended.

# File lib/prawn/document/snapshot.rb, line 48
def take_snapshot
  {:page_content    => Marshal.load(Marshal.dump(page.content)),
   :current_page    => Marshal.load(Marshal.dump(page.dictionary)),
   :page_number     => @page_number,
   :page_kids       => @store.pages.data[:Kids].map{|kid| kid.identifier},
   :dests           => names? && 
                       Marshal.load(Marshal.dump(names.data[:Dests]))}
end