Skip to content

Commit

Permalink
Added "do-exercise" command
Browse files Browse the repository at this point in the history
Runs a single exercise.  Reads the model name out of the header and looks for a
class with that name.
  • Loading branch information
dpwright committed Jul 7, 2012
1 parent 5d0b557 commit 74a7c8c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/srs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'srs/cli/help'
require 'srs/cli/insert-into'
require 'srs/cli/schedule'
require 'srs/cli/do-exercise'
require 'srs/cli/cat'

module SRS
Expand All @@ -10,6 +11,7 @@ class << self
COMMANDS = { "init" => :Init,
"insert-into" => :InsertInto,
"schedule" => :Schedule,
"do-exercise" => :DoExercise,
"cat" => :Cat,
"help" => :Help }.freeze

Expand Down
76 changes: 76 additions & 0 deletions lib/srs/cli/do-exercise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module SRS
class CLI
class DoExercise
def run!(arguments)
if not SRS::Workspace.initialised? then
puts "Current directory is not an SRS Workspace"
return 3
end

sha1 = arguments.shift
sha1_start = sha1[0..1]
sha1_rest = sha1[2..-1]

datafile = "exercises/#{sha1_start}/#{sha1_rest}"

if not File.exists?(datafile) then
puts "No content with that ID exists"
return 4
end

headers = {}
metadata = ""
File.open(datafile, "r") do |file|
while( line = file.gets() ) do
if line.strip.empty? then
break
end

keyval = line.split(':').map{|e| e.strip}
key = keyval[0]
val = keyval[1]

headers[key] = val
end
metadata = file.read
end

runModel(sha1, headers, metadata)
end

def runModel(sha1, headers, metadata)
if not headers.has_key?("Model") then
puts "Exercise #{sha1} has no model!\n"
return 6
end

modelclass = headers.delete("Model")

begin
require "./models/#{modelclass}"
rescue LoadError
begin
require "srs/models/#{modelclass}"
rescue LoadError
puts "Couldn't find model #{modelclass}."
return 7
end
end

model = SRS::Models.const_get(modelclass.to_sym).new
score = model.run(headers, metadata)

return score == nil ? 0 : score
end

def help()
puts <<-EOF
srs do-exercise <id>
Runs the exercise defined in <id>
EOF
end
end
end
end

1 change: 1 addition & 0 deletions lib/srs/cli/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def summary()
puts " init"
puts " insert-into"
puts " schedule"
puts " do-exercise"
puts " cat"
puts " help"
end
Expand Down

0 comments on commit 74a7c8c

Please sign in to comment.