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

Class Method Details

.empty?(hash) ⇒ Boolean

Check if a localized string hash is nil or empty.

Parameters:

  • hash (Hash, nil)

Returns:

  • (Boolean)


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.

Parameters:

  • hash (Hash, nil)

    the localized string hash

  • lang (String, Symbol)

    the desired language code

  • fallback (String, nil) (defaults to: "eng")

    fallback language (default “eng”)

Returns:

  • (String, nil)

    the localized value, or nil if not found



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.

Parameters:

  • hash (Hash, nil)

Returns:

  • (Boolean)


40
41
42
# File 'lib/glossarist/localized_string.rb', line 40

def self.present?(hash)
  !empty?(hash)
end