Class: TeachingPrintables::GridSheet

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

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.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 21

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.



10
11
12
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 10

def column_width
  @column_width
end

#columnsObject (readonly)

Returns the value of attribute columns.



10
11
12
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 10

def columns
  @columns
end

#gutterObject (readonly)

Returns the value of attribute gutter.



10
11
12
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 10

def gutter
  @gutter
end

#row_heightObject (readonly)

Returns the value of attribute row_height.



10
11
12
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 10

def row_height
  @row_height
end

#rowsObject (readonly)

Returns the value of attribute rows.



10
11
12
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 10

def rows
  @rows
end

Instance Method Details

#add_content(content_array) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 41

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



59
60
61
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 59

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

#update_grid_options(options) ⇒ Object



35
36
37
38
39
# File 'lib/teaching_printables/grid_sheet/grid_sheet.rb', line 35

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