Class: Judges::Judge

Inherits:
Object show all
Defined in:
lib/judges/judge.rb

Overview

A single judge.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024-2026 Yegor Bugayenko

License

MIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, lib, loog, epoch: Time.now) ⇒ Judge

Ctor.

Parameters:

  • dir (String)

    The directory with the judge

  • lib (String)

    The directory with the lib/

  • loog (Loog)

    The logging facility



26
27
28
29
30
31
# File 'lib/judges/judge.rb', line 26

def initialize(dir, lib, loog, epoch: Time.now)
  @dir = dir
  @lib = lib
  @loog = loog
  @epoch = epoch
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



20
21
22
# File 'lib/judges/judge.rb', line 20

def dir
  @dir
end

Instance Method Details

#nameString

Returns the name of the judge.

The name is derived from the directory name containing the judge.

Returns:

  • (String)

    The base name of the judge directory



104
105
106
# File 'lib/judges/judge.rb', line 104

def name
  File.basename(@dir)
end

#run(fb, global, local, options) ⇒ nil

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.

rubocop:disable Metrics/MethodLength

Parameters:

  • fb (Factbase)

    The factbase instance to operate on

  • global (Hash)

    Global configuration options shared across all judges

  • local (Hash)

    Local configuration options specific to this judge

  • options (Judges::Options)

    Command-line options object

Returns:

  • (nil)

    Nothing

Raises:

  • (RuntimeError)

    If the lib directory doesn’t exist, the script can’t be loaded, or execution fails



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/judges/judge.rb', line 61

def run(fb, global, local, options)
  $fb = fb
  $judge = File.basename(@dir)
  $options = options
  $loog = @loog
  $global = global
  $global.delete(:fb)
  $local = local
  $epoch = @epoch
  $kickoff = Time.now
  options.to_h.each { |k, v| ENV.store(k.to_s, v.to_s) }
  unless @lib.nil?
    raise(StandardError, "Lib dir #{@lib.to_rel} is absent") unless File.exist?(@lib)
    raise(StandardError, "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(StandardError, "Can't load '#{s}'") unless File.exist?(s)
  elapsed(@loog, good: "#{$judge} completed", level: Logger::INFO) do
    load(s, true)
    nil
    # rubocop:disable Lint/RescueException
  rescue Exception => e
    # rubocop:enable Lint/RescueException
    raise(e) if e.is_a?(RuntimeError) && e.message == 'skip'
    e = Judges::PrettyException.new(e) if e.is_a?(Octokit::ServerError)
    @loog.error(Backtrace.new(e))
    raise(e) if e.is_a?(StandardError)
    raise(e) if e.is_a?(Timeout::ExitException)
    raise(StandardError, "#{e.message} (#{e.class.name})")
  ensure
    $fb = $judge = $options = $loog = $epoch = $kickoff = nil
  end
end

#scriptString

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”.

Returns:

  • (String)

    The filename of the judge script (e.g., “judge_name.rb”)

Raises:

  • (RuntimeError)

    If the expected script file is not found in the judge directory



115
116
117
118
119
120
# File 'lib/judges/judge.rb', line 115

def script
  b = "#{File.basename(@dir)}.rb"
  files = Dir.glob(File.join(@dir, '*.rb')).map { |f| File.basename(f) }
  raise(StandardError, "No #{b} script in #{@dir.to_rel} among #{files}") unless files.include?(b)
  b
end

#testsArray<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.

Returns:

  • (Array<String>)

    Array of absolute paths to all .yml files in the judge directory



128
129
130
# File 'lib/judges/judge.rb', line 128

def tests
  Dir.glob(File.join(@dir, '*.yml'))
end

#to_sString

Returns the string representation of the judge.

Returns:

  • (String)

    The name of the judge (same as the directory name)



36
37
38
# File 'lib/judges/judge.rb', line 36

def to_s
  name
end

#with_loog(loog) ⇒ Judges::Judge

A new judge, with a different log.

Parameters:

  • loog (Loog)

    New log

Returns:



44
45
46
# File 'lib/judges/judge.rb', line 44

def with_loog(loog)
  Judges::Judge.new(@dir, @lib, loog, epoch: @epoch)
end