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: [], demote: []) ⇒ 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

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

    Names of judges to demote in priority



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

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

Instance Method Details

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

Iterates over all valid judges in the directory.

This method discovers all judge directories, validates them (ensuring they contain a corresponding .rb file), and yields them in a specific order. The order is determined by:

  1. Judges whose names match the boost list are placed first

  2. Judges whose names start with the shuffle prefix are randomly reordered

  3. Judges whose names match the demote list are placed last

  4. All other judges maintain their alphabetical order

Yields:

Returns:

  • (Enumerator)

    Returns an enumerator if no block is given



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
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/judges/judges.rb', line 72

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
  boosted = []
  demoted = []
  normal = []
  good.map { |a| a[0] }.each do |j|
    if @boost&.include?(j.name)
      boosted.append(j)
    elsif @demote&.include?(j.name)
      demoted.append(j)
    else
      normal.append(j)
    end
  end
  ret = boosted + normal + demoted
  ret.each(&)
end

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

Iterates over all judges while tracking their index position.

This method calls the #each method and additionally provides a zero-based index for each judge yielded. The judges are processed in the same order as determined by the #each method (with boost and shuffle rules applied).

Yields:

  • (Judges::Judge, Integer)

    Yields each judge object along with its index (starting from 0)

Returns:

  • (Integer)

    The total count of judges processed



117
118
119
120
121
122
123
124
# File 'lib/judges/judges.rb', line 117

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

#get(name) ⇒ Judges::Judge

Retrieves a specific judge by its name.

The judge must exist as a directory within the judges directory with the given name.

Parameters:

  • name (String)

    The name of the judge to retrieve (directory name)

Returns:

  • (Judges::Judge)

    The judge object initialized with the found directory

Raises:

  • (RuntimeError)

    If no judge directory exists with the given name



54
55
56
57
58
# File 'lib/judges/judges.rb', line 54

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