Class: Paperize::Layout

Inherits:
Object
  • Object
show all
Defined in:
lib/paperize/layout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Layout

Returns a new instance of Layout.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/paperize/layout.rb', line 6

def initialize(options={})
  @layout = options[:layout] || '3x3'
  @orientation = options[:orientation] || :portrait

  if layout == :card_per_page

  else
    @total_columns, @total_rows = layout.split('x').map(&:to_i) || [3, 3]
    @current_column = total_columns
    @current_row    = total_rows
  end
  @template       = options[:template]

  # page_size = layout == :card_per_page ? [2.5.in, 3.5.in] : 'LETTER'
  # @document = Prawn::Document.new(
  #   skip_page_creation: true,
  #   page_size: page_size,
  #   page_layout: @orientation
  # )
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



4
5
6
# File 'lib/paperize/layout.rb', line 4

def document
  @document
end

#layoutObject (readonly)

Returns the value of attribute layout.



3
4
5
# File 'lib/paperize/layout.rb', line 3

def layout
  @layout
end

#total_columnsObject (readonly)

Returns the value of attribute total_columns.



3
4
5
# File 'lib/paperize/layout.rb', line 3

def total_columns
  @total_columns
end

#total_rowsObject (readonly)

Returns the value of attribute total_rows.



3
4
5
# File 'lib/paperize/layout.rb', line 3

def total_rows
  @total_rows
end

Instance Method Details

#increment_current_cellObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/paperize/layout.rb', line 63

def increment_current_cell
  @current_column += 1

  if @current_column >= total_columns
    @current_column = 0
    @current_row += 1

    if @current_row >= total_rows
      @current_row = 0

      document.start_new_page
      document.define_grid(columns: total_columns, rows: total_rows)
    end
  end
end

#layout_cards(cards) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/paperize/layout.rb', line 36

def layout_cards(cards)
  card_margin = 0
  cards.each do |card|
    next_cell do
      shrink_bounds(2) do
        document.line_width = 2
        document.stroke_bounds
      end

      shrink_bounds(card_margin) do
        @template.render_block.call(document, card)
      end
    end
  end
end

#next_cell(&block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/paperize/layout.rb', line 52

def next_cell &block
  if layout == :card_per_page
    document.start_new_page
    document.canvas(&block)
  else
    increment_current_cell

    document.grid(@current_row, @current_column).bounding_box(&block)
  end
end

#shrink_bounds(margin, &block) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/paperize/layout.rb', line 27

def shrink_bounds(margin, &block)
  margin_left = margin
  margin_top = document.bounds.top - margin
  margin_width = document.bounds.width - 2*margin
  margin_height = document.bounds.height - 2*margin

  document.bounding_box [margin_left, margin_top], width: margin_width, height: margin_height, &block
end