Sha256: e306b7f9d62415f7192bce8319b8bfbc059f34087d9cc2c1df2947f4ef34dad9

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

# encoding: utf-8

# object_store.rb : Implements PDF object repository for Prawn
#
# Copyright August 2008, Brad Ediger.  All Rights Reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.
module Prawn
  class ObjectStore
    include Enumerable

    def initialize(info={})
      @objects = {}
      @identifiers = []
      
      # Create required PDF roots
      @info    = ref(info).identifier
      @pages   = ref(:Type => :Pages, :Count => 0, :Kids => []).identifier
      @root    = ref(:Type => :Catalog, :Pages => pages).identifier
    end

    def ref(data, &block)
      push(size + 1, data, &block)
    end                                               

    %w[info pages root].each do |name|
      define_method(name) do
        @objects[instance_variable_get("@#{name}")]
      end
    end

    # Adds the given reference to the store and returns the reference object.
    # If the object provided is not a Prawn::Reference, one is created from the
    # arguments provided.
    def push(*args, &block)
      reference = if args.first.is_a?(Prawn::Reference)
              args.first
            else
              Prawn::Reference.new(*args, &block)
            end
      @objects[reference.identifier] = reference
      @identifiers << reference.identifier
      reference
    end
    alias_method :<<, :push

    def each
      @identifiers.each do |id|
        yield @objects[id]
      end
    end

    def [](id)
      @objects[id]
    end

    def size
      @identifiers.size
    end
    alias_method :length, :size

  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
prawn-layout-0.3.2 vendor/prawn-core/lib/prawn/object_store.rb
prawn-layout-0.3.1 vendor/prawn-core/lib/prawn/object_store.rb
prawn-core-0.6.1 lib/prawn/object_store.rb