Sha256: 52d25f415215fa1a18fcbd2b2cb899c2c7d16c0f0d78c72c28f08be1cb3e8953

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

module PrawnCocktail
  class Renderer
    def initialize(template, data, doc_initializers)
      @template = template
      @data = data
      @doc_initializers = doc_initializers
    end

    def meta(opts)
      @prawn_document_options = opts
    end

    def content(&block)
      @doc_initializers.each do |proc|
        doc.instance_eval(&proc)
      end

      doc.instance_exec(data_object, &block)
    end

    def render_data
      render
      doc.render
    end

    def render_file(file)
      render
      doc.render_file(file)
    end

    private

    def render
      # We pass the filename for better backtraces.
      instance_eval(read_template, template_path)
    end

    def doc
      @doc ||= Prawn::Document.new(@prawn_document_options || {})
    end

    def data_object
      OpenStruct.new(@data)
    end

    def read_template
      File.read(File.join(app_root, template_path))
    end

    def template_path
      "app/views/documents/#{@template}.pdf.rb"
    end

    def app_root
      Rails.root
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prawn_cocktail-0.3.1 lib/prawn_cocktail/renderer.rb
prawn_cocktail-0.2.0 lib/prawn_cocktail/renderer.rb