Class: TeachingPrintables::GridSheet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Prawn::View
Defined in:
lib/teaching_printables/grid_sheet/grid_sheet.rb

Overview

Creates grid layouts for flashcards or math sheets. Delegates Prawn methods to Prawn::Document.

Direct Known Subclasses

PictureGridSheet

Constant Summary collapse

GRID_OPTIONS_DEFAULT =
{
  columns: 2,
  rows: 2,
  gutter: 0,
  column_width: 100,
  row_height: 100
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, doc = nil) ⇒ GridSheet

Returns a new instance of GridSheet.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 30

def initialize(args={},doc=nil)
  if !doc
    @document = Prawn::Document.new(args.merge({skip_page_creation: true }))
  elsif doc.kind_of?(Prawn::Document)
    @document = doc
  else
    raise ArgumentError, "Expecting Prawn::Document", caller
  end
  @font_size = 60
  update_grid_options(GRID_OPTIONS_DEFAULT.merge(args))
end

Instance Attribute Details

#column_widthObject (readonly)

Returns the value of attribute column_width.



19
20
21
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 19

def column_width
  @column_width
end

#columnsObject (readonly)

Returns the value of attribute columns.



19
20
21
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 19

def columns
  @columns
end

#gutterObject (readonly)

Returns the value of attribute gutter.



19
20
21
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 19

def gutter
  @gutter
end

#row_heightObject (readonly)

Returns the value of attribute row_height.



19
20
21
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 19

def row_height
  @row_height
end

#rowsObject (readonly)

Returns the value of attribute rows.



19
20
21
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 19

def rows
  @rows
end

Instance Method Details

#add_content(content_array) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 50

def add_content(content_array)
  @document.define_grid(grid_options)
  content_array.each_with_index {|obj,ind|
  
    if ind%(@rows*@columns)==0
      @document.start_new_page
    end
    
    subs = ind2sub([@rows,@columns],ind%(@rows*@columns))
    @document.grid(subs[0],subs[1]).bounding_box do
      #place_contents_in_gridbox(obj)
      @document.text_box obj.to_s, align: :center, valign: :center, size: @font_size, overflow: :shrink_to_fit
    end
  }
end

#place_contents_in_gridbox(obj) ⇒ Object



68
69
70
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 68

def place_contents_in_gridbox(obj)
  text obj.to_s, fit: [@column_width,@row_height]
end

#update_grid_options(options) ⇒ Object



44
45
46
47
48
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 44

def update_grid_options(options)
  options.each do |k,v|
    instance_variable_set("@#{k}", options[k]) unless options[k].nil?
  end
end