Class: Judges::Judges

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

Overview

Collection of all judges to run.

In the directory dir the following structure must be maintained:

dir/
  judge-one/
    judge-one.rb
    other files...
  judge-two/
    judge-two.rb
    other files...

The name of a directory of a judge must be exactly the same as the name of the .rb script inside the directory.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(dir, lib, loog, start: Time.now, shuffle: '', boost: []) ⇒ Judges

Initialize.

Parameters:

  • dir (String)

    Directory containing judges

  • lib (String)

    Library directory

  • loog (Loog)

    Logging facility

  • start (Time) (defaults to: Time.now)

    Start time

  • shuffle (String) (defaults to: '')

    Prefix for names of judges to shuffle

  • boost (Array<String>) (defaults to: [])

    Names of judges to boost in priority



36
37
38
39
40
41
42
43
# File 'lib/judges/judges.rb', line 36

def initialize(dir, lib, loog, start: Time.now, shuffle: '', boost: [])
  @dir = dir
  @lib = lib
  @loog = loog
  @start = start
  @shuffle = shuffle || ''
  @boost = boost
end

Instance Method Details

#each {|Judge| ... } ⇒ Enumerator

Iterate over all judges.

Yields:

  • (Judge)

    Yields each judge

Returns:

  • (Enumerator)

    If no block given



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
83
84
85
86
87
88
# File 'lib/judges/judges.rb', line 58

def each(&)
  return to_enum(__method__) unless block_given?
  list =
    Dir.glob(File.join(@dir, '*')).each.to_a.map do |d|
      next unless File.directory?(d)
      b = File.basename(d)
      next unless File.exist?(File.join(d, "#{b}.rb"))
      Judges::Judge.new(File.absolute_path(d), @lib, @loog)
    end
  list.compact!
  list.sort_by!(&:name)
  all = list.each_with_index.to_a
  good = all.dup
  mapping = all
    .map { |a| [a[0].name, a[1], a[1]] }
    .reject { |a| a[0].start_with?(@shuffle) }
    .to_h { |a| [a[1], a[2]] }
  positions = mapping.values.shuffle
  mapping.keys.zip(positions).to_h.each do |before, after|
    good[after] = all[before]
  end
  ret = []
  good.map { |a| a[0] }.each do |j|
    if @boost&.include?(j.name)
      ret.prepend(j)
    else
      ret.append(j)
    end
  end
  ret.each(&)
end

#each_with_index {|Judge, Integer| ... } ⇒ Integer

Iterate over all judges with index.

Yields:

  • (Judge, Integer)

    Yields each judge with its index

Returns:

  • (Integer)

    The total count of judges



93
94
95
96
97
98
99
100
# File 'lib/judges/judges.rb', line 93

def each_with_index
  idx = 0
  each do |p|
    yield [p, idx]
    idx += 1
  end
  idx
end

#get(name) ⇒ Judge

Get one judge by name.

Parameters:

  • name (String)

    The name of the judge

Returns:

  • (Judge)

    The judge object

Raises:

  • (RuntimeError)

    If judge doesn’t exist



49
50
51
52
53
# File 'lib/judges/judges.rb', line 49

def get(name)
  d = File.absolute_path(File.join(@dir, name))
  raise "Judge #{name} doesn't exist in #{@dir}" unless File.exist?(d)
  Judges::Judge.new(d, @lib, @loog, start: @start)
end