Class: Judges::Options

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

Overview

Options for Ruby scripts in the judges.

This class manages configuration options that can be passed to judge scripts. Options are key-value pairs that can be provided in various formats and are normalized into a hash with symbol keys. Values are automatically converted to appropriate types (integers for numeric strings, etc.).

The class also provides dynamic method access to options using the ‘others’ gem, allowing options to be accessed as method calls.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(pairs = nil) ⇒ Options

Initialize a new Options instance.

Examples:

Initialize with array

options = Judges::Options.new(["token=abc123", "debug=true"])

Initialize with string

options = Judges::Options.new("token=abc123,debug")

Initialize with hash

options = Judges::Options.new({ token: "abc123", debug: true })

Parameters:

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

    List of key-value pairs. Can be provided as:

    • Array of strings: [“token=af73cd3”, “max_speed=1”]

    • Comma-separated string: “token=af73cd3,max_speed=1”

    • Hash: { token: “af73cd3”, max_speed: 1 }

    • nil: Creates empty options



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

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

Instance Method Details

#+(other) ⇒ Judges::Options

Merge with another Options object.

Creates a new Options instance containing values from both this instance and the other. Values from the other Options object override values with the same key in this instance.

Examples:

Merge two Options objects

opts1 = Judges::Options.new(["token=abc", "debug=true"])
opts2 = Judges::Options.new(["token=xyz", "verbose=true"])
merged = opts1 + opts2
# merged now has token=xyz, debug=true, verbose=true

Parameters:

Returns:



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

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

#empty?Boolean

Check if options are empty.

Examples:

Check if options are empty

options = Judges::Options.new
options.empty? # => true
options = Judges::Options.new(["key=value"])
options.empty? # => false

Returns:

  • (Boolean)

    true if no options are set, false otherwise



49
50
51
# File 'lib/judges/options.rb', line 49

def empty?
  to_h.empty?
end

#to_hHash

Convert options to hash.

Converts the raw pairs into a normalized hash with the following transformations:

  • Keys are converted to uppercase symbols

  • Values without equals sign get ‘true’ as value

  • Numeric strings are converted to integers

  • Leading/trailing whitespace is stripped

  • Empty or nil keys are rejected

Examples:

Convert to hash

options = Judges::Options.new("token=abc123,max_speed=100,debug")
options.to_h # => { TOKEN: "abc123", MAX_SPEED: 100, DEBUG: "true" }

Returns:

  • (Hash)

    The options as a hash with symbol keys



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/judges/options.rb', line 108

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_sString

Convert options to a string representation.

Creates a human-readable string representation of all options, suitable for logging. Sensitive values (longer than 8 characters) are partially masked with asterisks for security.

Examples:

Convert to string

options = Judges::Options.new(["token=supersecrettoken", "debug=true"])
puts options.to_s
# Output:
# debug → "true"
# token → "supe****oken"

Returns:

  • (String)

    Formatted string with each option on a new line



87
88
89
90
91
92
93
# File 'lib/judges/options.rb', line 87

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