module Scoring # Normalizza `score_states.data["period"]` (intero o etichetta "Q2" / "2° tempo"). module PeriodData module_function def number_from(raw) case raw when Integer, Float value = raw.to_i value.positive? ? value : nil when String text = raw.strip return nil if text.empty? if (match = text.match(/\AQ(\d+)\z/i)) match[1].to_i elsif (match = text.match(/\A(\d+)\s*°/)) match[1].to_i elsif (value = text.to_i).positive? value end end end def normalize_hash!(data) number = number_from(data["period"]) data["period"] = number if number&.positive? data["period"] = 1 unless data["period"].to_i.positive? data end end end