class Glossarist::Concept

Attributes

id[RW]

Concept ID. @return [String]

localizations[R]

All localizations for this concept.

Keys are language codes and values are instances of {LocalizedConcept}. @return [Hash<String, LocalizedConcept>]

Public Class Methods

from_h(hash) click to toggle source

rubocop:disable Metrics/AbcSize, Style/RescueModifier

# File lib/glossarist/concept.rb, line 56
def self.from_h(hash)
  new.tap do |concept|
    concept.id = hash.dig("termid")

    hash.values
      .grep(Hash)
      .map { |subhash| LocalizedConcept.from_h(subhash) rescue nil }
      .compact
      .each { |lc| concept.add_l10n lc }

    concept.l10n("eng")&.superseded_concepts = hash.dig("related") || []
  end
end
new(*) click to toggle source
Calls superclass method
# File lib/glossarist/concept.rb, line 18
def initialize(*)
  @localizations = {}
  super
end

Public Instance Methods

add_l10n(localized_concept)
Alias for: add_localization
add_localization(localized_concept) click to toggle source

Adds concept localization. @param localized_concept [LocalizedConcept]

# File lib/glossarist/concept.rb, line 25
def add_localization(localized_concept)
  lang = localized_concept.language_code
  localizations.store(lang, localized_concept)
end
Also aliased as: add_l10n
default_designation() click to toggle source

rubocop:enable Metrics/AbcSize, Style/RescueModifier

# File lib/glossarist/concept.rb, line 71
def default_designation
  localized = localization("eng") || localizations.values.first
  localized&.terms&.first&.designation
end
l10n(lang)
Alias for: localization
localization(lang) click to toggle source

Returns concept localization. @param lang [String] language code @return [LocalizedConcept]

# File lib/glossarist/concept.rb, line 35
def localization(lang)
  localizations[lang]
end
Also aliased as: l10n
to_h() click to toggle source
# File lib/glossarist/concept.rb, line 41
def to_h
  {
    "termid" => id,
    "term" => default_designation,
    "related" => related_concepts,
  }
  .compact
  .merge(localizations.transform_values(&:to_h))
end
Also aliased as: to_hash
to_hash()

@deprecated For legacy reasons only.

Implicit conversion (i.e. {#to_hash} alias) will be removed soon.
Alias for: to_h