Sha256: 20074d21f1bf1b09be848bcbb2491a5840bdb236c3153974b80227ea286a6c09

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

module DotGrid
  class Document
    attr_accessor(
      :document,
      :pdf,
      :file_name,
      :orientation,
      :page_size,
      :margin,
      :pages,
      :page_types
    )

    def initialize(params = {})
      @file_name = params[:file_name] || "dotgrid.pdf"
      @page_size = params[:page_size] ? parse_page_size(params[:page_size]) : "LETTER"
      @orientation = params[:orientation] ? params[:orientation].downcase.to_sym : :portrait
      @margin = params[:margin] || 0.0
      @page_types = params[:page_types] ? params[:page_types].downcase.split(",").map { |w| w.strip } : ["planner"]

      @pdf = Prawn::Document.new(margin: margin, page_size: page_size, skip_page_creation: true, page_layout: orientation)
      @pages = create_pages(params.merge({pdf: pdf}))
    end

    def parse_page_size(page_size)
      return page_size unless p = /(?'w'\d+\.{0,1}\d*)x(?'h'\d+\.{0,1}\d*)(?'u'[a-z]{2})/.match(page_size)
      return [p[:w].to_f.send(p[:u]), p[:h].to_f.send(p[:u])]
    end

    def create_pages(params)
      page_types.map do |p|
        DotGrid::Page::Factory.build(p, params.clone)
      end
    end

    def generate
      pages.each { |page| page.generate }
      pdf.render_file file_name
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dot_grid-0.0.12 lib/dot_grid/document.rb
dot_grid-0.0.11 lib/dot_grid/document.rb
dot_grid-0.0.10 lib/dot_grid/document.rb