This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
/
auto_grader.rb
77 lines (63 loc) · 2.46 KB
/
auto_grader.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'ruby-debug'
class AutoGrader
class AutoGrader::NoSuchGraderError < StandardError ; end
# ==== Attributes
# textual feedback from the autograder on the student's answer
attr_accessor :comments
# errors running the autograder, if any; else nil
attr_accessor :errors
# identifier of question being graded
attr_accessor :assignment_id
# achieved raw score as reported by the underlying grader
attr_reader :raw_score
# the maximum possible raw score as reported by the underlying grader
attr_reader :raw_max
protected :raw_score, :raw_max
# Create a new autograder object, which will grade a student's submission
# given the submission text, a grading strategy, and grading rules to be
# used by that strategy.
# * +assignment_id+ - string identifier for question being graded
# * +grader+ - grading strategy to use. Currently only +:rspec_grader+ is
# valid. Raises +AutoGrader::NoSuchGraderError+ if nonexistent strategy
# specified.
# * +submitted_answer+ - a string containing the student's submitted answer,
# such as code.
# * +grading_rules+ - a hash containing the grading options supported by the
# chosen grading strategy. See each strategy's class for what options are
# expected or required by that strategy.
# * +normalize+ - if given, normalize score to this maximum; default 100
def self.create(assignment_id, grader, submitted_answer, grading_rules, normalize=100)
@@initialized ||= AutoGrader.class_init
if submitted_answer.nil? || submitted_answer.empty?
AutoGrader.new(assignment_id)
else
begin
obj = Object.const_get(grader).send(:new, submitted_answer, grading_rules)
obj.assignment_id = assignment_id
return obj
rescue NameError => e
raise AutoGrader::NoSuchGraderError, "Can't find grading strategy for #{grader}"
end
end
end
def normalized_score(max=100)
raw_max.zero? ? 0 : (max.to_f * raw_score/raw_max).ceil
end
# Grade the given question using the specified grader, strategy, and
# maximum score. If all
def grade!
# default method does nothing and leaves a score of 0
end
private
def self.class_init
Dir["lib/graders/*_grader/*.rb"].each { |file| load file }
end
#:nodoc: not to be used externally
private
def initialize(assignment_id)
@raw_max = @raw_score = 0
@comments = 'You did not submit any answer.'
@assignment_id = assignment_id
@errors = nil
end
end