Class: Judges::Options

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

Overview

Options for Ruby scripts in the judges.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(pairs) ⇒ Options

Ctor.

Parameters:

  • pairs (Array<String>)

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



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

def initialize(pairs)
  @pairs = pairs
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object

Get option by name.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/judges/options.rb', line 47

def method_missing(*args)
  @hash ||= begin
    pp = @pairs || []
    pp = @pairs.map { |k, v| "#{k}=#{v}" } if pp.is_a?(Hash)
    pp.to_h do |pair|
      p = pair.split('=', 2)
      [p[0].to_sym, p[1].match?(/^[0-9]+$/) ? p[1].to_i : p[1]]
    end
  end
  k = args[0].downcase
  @hash[k]
end

Instance Method Details

#respond_to?(_method, _include_private = false) ⇒ Boolean

rubocop:disable Style/OptionalBooleanParameter

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/judges/options.rb', line 61

def respond_to?(_method, _include_private = false)
  # rubocop:enable Style/OptionalBooleanParameter
  true
end

#respond_to_missing?(_method, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/judges/options.rb', line 66

def respond_to_missing?(_method, _include_private = false)
  true
end

#to_sObject

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



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

def to_s
  touch # this will trigger method_missing() method, which will create @hash
  @hash.map do |k, v|
    v = v.to_s
    v = "#{v[0..3]}#{'*' * (v.length - 4)}" if v.length > 8
    "#{k}=#{v}"
  end.join("\n")
end