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



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

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

Instance Method Details

#+(other) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/judges/options.rb', line 41

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)


37
38
39
# File 'lib/judges/options.rb', line 37

def empty?
  to_h.empty?
end

#to_hObject



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

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



50
51
52
53
54
55
56
# File 'lib/judges/options.rb', line 50

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