forked from autolab/Autolab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.rb
executable file
·47 lines (39 loc) · 1.26 KB
/
utilities.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
module Utilities
def self.serializable(attributes, serializable)
attributes.keep_if { |f, _| serializable.include? f }
attributes.delete_if { |_, v| v.nil? }
attributes
end
def self.is_url?(name)
require "uri"
uri = URI.parse(name)
return false if uri.nil? || uri.scheme.nil? || uri.scheme.empty?
return true
rescue URI::InvalidURIError
return false
end
def self.execute_instructor_code(invoked_method_name)
yield
rescue Exception => e
raise InstructorException.new("Error executing #{invoked_method_name}: #{e}")
end
def self.validated_score_value(score, invoked_method_name, allow_nil = false)
message = "Error executing #{invoked_method_name}"
if score
if (score = Float(score) rescue nil)
fail InvalidComputedScoreException.new("#{message}: returned infinite number") unless score.finite?
else
fail InvalidComputedScoreException.new("#{message}: error converting to float")
end
else
fail InvalidComputedScoreException.new("#{message}: returned nil") unless allow_nil
end
score
end
end
class ScoreComputationException < StandardError
end
class InvalidComputedScoreException < ScoreComputationException
end
class InstructorException < ScoreComputationException
end