Class: Judges::Options
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
-
#+(other) ⇒ Judges::Options
Merge with another Options object.
-
#empty? ⇒ Boolean
Check if options are empty.
-
#initialize(pairs = nil) ⇒ Options
constructor
Initialize a new Options instance.
-
#to_h ⇒ Hash
Convert options to hash.
-
#to_s ⇒ String
Convert options to a string representation.
Constructor Details
#initialize(pairs = nil) ⇒ Options
Initialize a new Options instance.
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.
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.
49 50 51 |
# File 'lib/judges/options.rb', line 49 def empty? to_h.empty? end |
#to_h ⇒ Hash
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
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_s ⇒ String
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.
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 |