Class: Spirit::Render::Table

Inherits:
Problem show all
Defined in:
lib/spirit/render/templates/table.rb

Overview

Renders table problems marked up in YAML as HTML.

The grid should be given as a 2-dimensional array that represents the table to be filled in. “?” is a special token used in the grid to indicate cells that require student input.

The answer should also be given as a 2-dimensional array. However, a dummy token may be used in cells that do not require student input to cut redundancy. In the example below, the “-” token is used.

Examples:

{
  "format": "table",
  "question": "fill me in",
  "grid": [[0, "?", 2], [3, "?", 5]],
  "answer": [["-", 1, "-"],  ["-", 4, "-"]
}

Constant Summary collapse

HEADINGS =

Optional headings key.

'headings'
GRID =

Required grid key.

'grid'
FILL_ME_IN =

Special token indicating that the cell should be filled in.

'?'

Constants inherited from Problem

Problem::ANSWER, Problem::FORMAT, Problem::ID, Problem::KEYS, Problem::QUESTION

Instance Attribute Summary

Attributes inherited from Problem

#digest, #id, #nesting

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Problem

accessor, get_instance, parse, #render

Methods inherited from Template

#render

Constructor Details

#initialize(yaml, digest) ⇒ Table

Ensures that the headings key exists.



40
41
42
43
# File 'lib/spirit/render/templates/table.rb', line 40

def initialize(yaml, digest)
  yaml[HEADINGS] ||= nil
  super
end

Class Method Details

.input?(cell) ⇒ Boolean

Returns true iff the given cell is an input cell.

Returns:

  • (Boolean)

    true iff the given cell is an input cell



61
62
63
# File 'lib/spirit/render/templates/table.rb', line 61

def self.input?(cell)
  cell == FILL_ME_IN
end

Instance Method Details

#answerHash

Gets the expected answer, in www-form-urlencoded format.

Returns:

  • (Hash)

    answers, as expected from student’s form submission



55
56
57
58
# File 'lib/spirit/render/templates/table.rb', line 55

def answer
  return @answer unless @answer.nil?
  @answer = encode_answer
end

#valid?Boolean

Checks if the given yaml contains a valid table.

Returns:

  • (Boolean)

    true iff the yaml contains a valid table.



47
48
49
50
51
# File 'lib/spirit/render/templates/table.rb', line 47

def valid?
  super and
    valid_grid? and
    valid_answer?
end