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-2025 Yegor Bugayenko

License

MIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Ctor.

Parameters:

  • dir (String)

    The directory with the judge

  • lib (String)

    The directory with the lib/

  • loog (Loog)

    The logging facility



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

#dirObject (readonly)

Returns the value of attribute dir.



18
19
20
# File 'lib/judges/judge.rb', line 18

def dir
  @dir
end

Instance Method Details

#nameObject

Get the name of the judge.



78
79
80
# File 'lib/judges/judge.rb', line 78

def name
  File.basename(@dir)
end

#run(fb, global, local, options) ⇒ Factbase::Churn

Run it with the given Factbase and environment variables.

Parameters:

  • fb (Factbase)

    The factbase

  • global (Hash)

    Global options

  • local (Hash)

    Local options

  • options (Judges::Options)

    The options from command line

Returns:

  • (Factbase::Churn)

    The changes just made



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
# File 'lib/judges/judge.rb', line 44

def run(fb, global, local, options)
  $fb = Factbase::Tallied.new(fb)
  $judge = File.basename(@dir)
  $options = options
  $loog = @loog
  $global = global
  $local = local
  $start = @start
  options.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 = $options = $loog = nil
  end
end

#scriptObject

Get the name of the .rb script in the judge.



83
84
85
86
87
88
# File 'lib/judges/judge.rb', line 83

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

#testsObject

Return all .yml tests files.



91
92
93
# File 'lib/judges/judge.rb', line 91

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

#to_sString

Print it as a string.

Returns:

  • (String)

    Name of it



33
34
35
# File 'lib/judges/judge.rb', line 33

def to_s
  name
end