Class: DotGrid::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/dot_grid/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Document

Returns a new instance of Document.



14
15
16
17
18
19
20
21
22
23
# File 'lib/dot_grid/document.rb', line 14

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

Instance Attribute Details

#documentObject

Returns the value of attribute document.



3
4
5
# File 'lib/dot_grid/document.rb', line 3

def document
  @document
end

#file_nameObject

Returns the value of attribute file_name.



3
4
5
# File 'lib/dot_grid/document.rb', line 3

def file_name
  @file_name
end

#marginObject

Returns the value of attribute margin.



3
4
5
# File 'lib/dot_grid/document.rb', line 3

def margin
  @margin
end

#orientationObject

Returns the value of attribute orientation.



3
4
5
# File 'lib/dot_grid/document.rb', line 3

def orientation
  @orientation
end

#page_sizeObject

Returns the value of attribute page_size.



3
4
5
# File 'lib/dot_grid/document.rb', line 3

def page_size
  @page_size
end

#page_typesObject

Returns the value of attribute page_types.



3
4
5
# File 'lib/dot_grid/document.rb', line 3

def page_types
  @page_types
end

#pagesObject

Returns the value of attribute pages.



3
4
5
# File 'lib/dot_grid/document.rb', line 3

def pages
  @pages
end

#pdfObject

Returns the value of attribute pdf.



3
4
5
# File 'lib/dot_grid/document.rb', line 3

def pdf
  @pdf
end

Instance Method Details

#create_pages(params) ⇒ Object



30
31
32
33
34
# File 'lib/dot_grid/document.rb', line 30

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

#generateObject



36
37
38
39
# File 'lib/dot_grid/document.rb', line 36

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

#parse_page_size(page_size) ⇒ Object



25
26
27
28
# File 'lib/dot_grid/document.rb', line 25

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