Class: Judges::Options
Overview
Options for Ruby scripts in the judges.
- Author
-
Yegor Bugayenko (yegor256@gmail.com)
- 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
Ctor.
-
#to_h ⇒ Hash
Convert options to hash.
-
#to_s ⇒ Object
Convert them all to a string (printable in a log).
Constructor Details
#initialize(pairs = nil) ⇒ Options
Ctor.
16 17 18 |
# File 'lib/judges/options.rb', line 16 def initialize(pairs = nil) @pairs = pairs end |
Instance Method Details
#+(other) ⇒ Judges::Options
Merge with another Options object.
29 30 31 32 33 34 35 |
# File 'lib/judges/options.rb', line 29 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.
22 23 24 |
# File 'lib/judges/options.rb', line 22 def empty? to_h.empty? end |
#to_h ⇒ Hash
Convert options to hash.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/judges/options.rb', line 48 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 ⇒ Object
Convert them all to a string (printable in a log).
38 39 40 41 42 43 44 |
# File 'lib/judges/options.rb', line 38 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 |