Class: Judges::Options

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

Overview

Options for Ruby scripts in the judges.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(pairs = nil) ⇒ Options

Ctor.

Parameters:

  • pairs (Array<String>) (defaults to: nil)

    List of pairs, like [“token=af73cd3”, “max_speed=1”]



16
17
18
# File 'lib/judges/options.rb', line 16

def initialize(pairs = nil)
  @pairs = pairs
end

Instance Method Details

#+(other) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/judges/options.rb', line 24

def +(other)
  h = to_h
  other.to_h.each do |k, v|
    h[k] = v
  end
  Judges::Options.new(h)
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  to_h.empty?
end

#to_hObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/judges/options.rb', line 41

def to_h
  @to_h ||=
    begin
      pp = @pairs || []
      pp = pp.split(',') if pp.is_a?(String)
      if pp.is_a?(Array)
        pp = pp
          .compact
          .map(&:strip)
          .reject(&:empty?)
          .map { |s| s.split('=', 2) }
          .map { |a| a.size == 1 ? [a[0], nil] : a }
          .reject { |a| a[0].empty? }
          .to_h
      end
      pp
        .reject { |k, _| k.nil? }
        .reject { |k, _| k.is_a?(String) && k.empty? }
        .to_h
        .transform_values { |v| v.nil? ? 'true' : v }
        .transform_values { |v| v.is_a?(String) ? v.strip : v }
        .transform_values { |v| v.is_a?(String) && v.match?(/^[0-9]+$/) ? v.to_i : v }
        .transform_keys { |k| k.to_s.strip.upcase.to_sym }
    end
end

#to_sObject

Convert them all to a string (printable in a log).



33
34
35
36
37
38
39
# File 'lib/judges/options.rb', line 33

def to_s
  to_h.map do |k, v|
    v = v.to_s
    v = "#{v[0..3]}#{'*' * (v.length - 8)}#{v[-4..]}" if v.length > 8
    "#{k} → \"#{v}\""
  end.sort.join("\n")
end