Class: Judges::Options
Overview
Options for Ruby scripts in the judges.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
- #+(other) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(pairs = nil) ⇒ Options
constructor
Ctor.
- #to_h ⇒ Object
-
#to_s ⇒ Object
Convert them all to a string (printable in a log).
Constructor Details
#initialize(pairs = nil) ⇒ Options
Ctor.
33 34 35 |
# File 'lib/judges/options.rb', line 33 def initialize(pairs = nil) @pairs = pairs end |
Instance Method Details
#+(other) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/judges/options.rb', line 41 def +(other) h = to_h other.to_h.each do |k, v| h[k] = v end Judges::Options.new(h) end |
#empty? ⇒ Boolean
37 38 39 |
# File 'lib/judges/options.rb', line 37 def empty? to_h.empty? end |
#to_h ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/judges/options.rb', line 58 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).
50 51 52 53 54 55 56 |
# File 'lib/judges/options.rb', line 50 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 |