Class: Judges::Judge
Overview
A single judge.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024-2025 Yegor Bugayenko
- License
-
MIT
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
Instance Method Summary collapse
-
#initialize(dir, lib, loog, start: Time.now) ⇒ Judge
constructor
Ctor.
-
#name ⇒ String
Returns the name of the judge.
-
#run(fb, global, local, options) ⇒ Factbase::Churn
Executes the judge script with the provided factbase and configuration.
-
#script ⇒ String
Returns the name of the main Ruby script file for this judge.
-
#tests ⇒ Array<String>
Returns all YAML test files in the judge directory.
-
#to_s ⇒ String
Returns the string representation of the judge.
Constructor Details
#initialize(dir, lib, loog, start: Time.now) ⇒ Judge
Ctor.
24 25 26 27 28 29 |
# File 'lib/judges/judge.rb', line 24 def initialize(dir, lib, loog, start: Time.now) @dir = dir @lib = lib @loog = loog @start = start end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
18 19 20 |
# File 'lib/judges/judge.rb', line 18 def dir @dir end |
Instance Method Details
#name ⇒ String
Returns the name of the judge.
The name is derived from the directory name containing the judge.
89 90 91 |
# File 'lib/judges/judge.rb', line 89 def name File.basename(@dir) end |
#run(fb, global, local, options) ⇒ Factbase::Churn
Executes the judge script with the provided factbase and configuration.
This method sets up the execution environment by creating global variables, loading library files, and running the judge script. It tracks execution time and captures any errors that occur during execution.
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 77 78 79 80 81 82 |
# File 'lib/judges/judge.rb', line 50 def run(fb, global, local, ) $fb = Factbase::Tallied.new(fb) $judge = File.basename(@dir) = $loog = @loog $global = global $global.delete(:fb) # to make sure Tallied is always actual $local = local $start = @start .to_h.each { |k, v| ENV.store(k.to_s, v.to_s) } unless @lib.nil? raise "Lib dir #{@lib.to_rel} is absent" unless File.exist?(@lib) raise "Lib #{@lib.to_rel} is not a directory" unless File.directory?(@lib) Dir.glob(File.join(@lib, '*.rb')).each do |f| require_relative(File.absolute_path(f)) end end s = File.join(@dir, script) raise "Can't load '#{s}'" unless File.exist?(s) elapsed(@loog, intro: "#{$judge} completed", level: Logger::INFO) do load(s, true) $fb.churn # rubocop:disable Lint/RescueException rescue Exception => e # rubocop:enable Lint/RescueException @loog.error(Backtrace.new(e)) raise e if e.is_a?(StandardError) raise e if e.is_a?(Timeout::ExitException) raise "#{e.message} (#{e.class.name})" ensure $fb = $judge = = $loog = nil end end |
#script ⇒ String
Returns the name of the main Ruby script file for this judge.
The script file must have the same name as the judge directory with a .rb extension. For example, if the judge directory is “quality”, the script must be “quality.rb”.
100 101 102 103 104 105 |
# File 'lib/judges/judge.rb', line 100 def script b = "#{File.basename(@dir)}.rb" files = Dir.glob(File.join(@dir, '*.rb')).map { |f| File.basename(f) } raise "No #{b} script in #{@dir.to_rel} among #{files}" unless files.include?(b) b end |
#tests ⇒ Array<String>
Returns all YAML test files in the judge directory.
Test files are expected to have a .yml extension and contain test data used to validate the judge’s behavior.
113 114 115 |
# File 'lib/judges/judge.rb', line 113 def tests Dir.glob(File.join(@dir, '*.yml')) end |
#to_s ⇒ String
Returns the string representation of the judge.
34 35 36 |
# File 'lib/judges/judge.rb', line 34 def to_s name end |