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 = nil) ⇒ Options

Ctor.

Parameters:

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

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



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

def initialize(pairs = nil)
  @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
59
60
61
62
63
64
# 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 = pp.split(',') if pp.is_a?(String)
    pp.compact!
    pp.reject!(&:empty?)
    pp.to_h do |pair|
      p = pair.split('=', 2)
      k = p[0].strip
      v = p[1]
      v = v.nil? ? 'true' : v.strip
      [k.to_sym, v.match?(/^[0-9]+$/) ? v.to_i : v]
    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)


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

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

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

Returns:

  • (Boolean)


72
73
74
# File 'lib/judges/options.rb', line 72

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