Class: SEPA::ProfileRegistry
- Inherits:
-
Object
- Object
- SEPA::ProfileRegistry
- Defined in:
- lib/sepa_rator/profile.rb
Class Method Summary collapse
- .[](profile_id) ⇒ Object
- .all ⇒ Object
- .known_country?(code) ⇒ Boolean
-
.recommended(family:, country: nil, version: :latest) ⇒ Object
Resolve a (family, country, version) triple to the recommended profile.
- .register(profile, aliases: []) ⇒ Object
-
.register_countries(*codes) ⇒ Object
Register one or more country codes as valid inputs to ‘Message.new(country:)`.
-
.set_country_default(family:, country:, version:, profile:) ⇒ Object
Register a (family, country, version) → profile mapping used by ‘Message.new(country:, version:)` to resolve the recommended profile.
Class Method Details
.[](profile_id) ⇒ Object
197 198 199 200 201 |
# File 'lib/sepa_rator/profile.rb', line 197 def [](profile_id) @profiles.fetch(profile_id.to_s) do raise ArgumentError, "Unknown profile: #{profile_id.inspect}" end end |
.all ⇒ Object
203 204 205 |
# File 'lib/sepa_rator/profile.rb', line 203 def all @profiles.values.uniq end |
.known_country?(code) ⇒ Boolean
214 215 216 |
# File 'lib/sepa_rator/profile.rb', line 214 def known_country?(code) code.nil? || @known_countries.include?(code) end |
.recommended(family:, country: nil, version: :latest) ⇒ Object
Resolve a (family, country, version) triple to the recommended profile. Falls back to the generic ‘country: nil` entry when the requested country is a known SEPA country without a dedicated profile.
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/sepa_rator/profile.rb', line 248 def recommended(family:, country: nil, version: :latest) raise SEPA::UnknownCountryError.new(country: country, known_countries: @known_countries.to_a) unless known_country?(country) per_country = @country_defaults.fetch(family) do raise ArgumentError, "Unknown family: #{family.inspect}" end fallback_used = per_country[country].nil? versions = per_country[country] || per_country[nil] raise ArgumentError, "No default profiles registered for family=#{family.inspect}" unless versions versions.fetch(version) do raise SEPA::UnsupportedVersionError.new( country: country, version: version, available_versions: versions.keys, fallback_used: fallback_used ) end end |
.register(profile, aliases: []) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/sepa_rator/profile.rb', line 185 def register(profile, aliases: []) ([profile.id] + aliases.map(&:to_s)).each do |key| next unless @profiles.key?(key) && !@profiles[key].equal?(profile) raise ArgumentError, "A different profile is already registered under #{key.inspect}" end @profiles[profile.id] = profile aliases.each { |name| @profiles[name.to_s] = profile } profile end |
.register_countries(*codes) ⇒ Object
Register one or more country codes as valid inputs to ‘Message.new(country:)`. Must be called from country_defaults.rb so that the allow-list is populated before any Message is constructed.
210 211 212 |
# File 'lib/sepa_rator/profile.rb', line 210 def register_countries(*codes) codes.each { |code| @known_countries << code } end |
.set_country_default(family:, country:, version:, profile:) ⇒ Object
Register a (family, country, version) → profile mapping used by ‘Message.new(country:, version:)` to resolve the recommended profile.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/sepa_rator/profile.rb', line 220 def set_country_default(family:, country:, version:, profile:) unless profile.family == family raise ArgumentError, "set_country_default: profile #{profile.id.inspect} is for family " \ "#{profile.family.inspect}, not #{family.inspect}" end @country_defaults[family] ||= {} @country_defaults[family][country] ||= {} if @country_defaults[family][country].key?(version) && !@country_defaults[family][country][version].equal?(profile) raise ArgumentError, "set_country_default: (#{family.inspect}, #{country.inspect}, #{version.inspect}) " \ "already maps to #{@country_defaults[family][country][version].id.inspect}" end @country_defaults[family][country][version] = profile end |