Module: Glossarist::LocalizedString
- Defined in:
- lib/glossarist/localized_string.rb
Overview
Semantic operations on localized string fields.
Localized strings are stored as hashes keyed by ISO 639 language code:
{ "eng" => "Mixed reflection", "fra" => "Réflexion mixte" }
This module provides typed access and fallback logic. It does not introduce a wrapper class — the hash IS the localized string, matching how Section#names, DatasetRegister#description, etc. already work.
Class Method Summary collapse
-
.empty?(hash) ⇒ Boolean
Check if a localized string hash is nil or empty.
-
.fetch(hash, lang, fallback = "eng") ⇒ String?
Fetch a localized value with language fallback.
-
.present?(hash) ⇒ Boolean
Check if a localized string hash has any entries.
Class Method Details
.empty?(hash) ⇒ Boolean
Check if a localized string hash is nil or empty.
32 33 34 |
# File 'lib/glossarist/localized_string.rb', line 32 def self.empty?(hash) hash.nil? || hash.empty? end |
.fetch(hash, lang, fallback = "eng") ⇒ String?
Fetch a localized value with language fallback.
19 20 21 22 23 24 25 26 |
# File 'lib/glossarist/localized_string.rb', line 19 def self.fetch(hash, lang, fallback = "eng") return nil unless hash.is_a?(Hash) direct = hash[lang.to_s] || hash[lang.to_sym] return direct if direct fallback && fallback.to_s != lang.to_s ? hash[fallback.to_s] || hash[fallback.to_sym] : nil end |
.present?(hash) ⇒ Boolean
Check if a localized string hash has any entries.
40 41 42 |
# File 'lib/glossarist/localized_string.rb', line 40 def self.present?(hash) !empty?(hash) end |