Class: Glossarist::ManagedConcept

Inherits:
Model
  • Object
show all
Includes:
Utilities::CommonFunctions, Utilities::Enum
Defined in:
lib/glossarist/managed_concept.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities::CommonFunctions

#slice_keys, #symbolize_keys

Methods included from Utilities::Enum

extended, included

Methods inherited from Model

from_h, new, #set_attribute

Constructor Details

#initialize(attributes = {}) ⇒ ManagedConcept

Returns a new instance of ManagedConcept.



30
31
32
33
34
35
36
# File 'lib/glossarist/managed_concept.rb', line 30

def initialize(attributes = {})
  @localizations = {}
  self.localized_concepts = attributes.values.grep(Hash)

  attributes = symbolize_keys(attributes)
  super(slice_keys(attributes, managed_concept_attributes))
end

Instance Attribute Details

#datesObject

return [Array<ConceptDate>]



19
20
21
# File 'lib/glossarist/managed_concept.rb', line 19

def dates
  @dates
end

#idString

Returns:

  • (String)


9
10
11
# File 'lib/glossarist/managed_concept.rb', line 9

def id
  @id
end

#localizationsHash<String, LocalizedConcept>

All localizations for this concept.

Keys are language codes and values are instances of LocalizedConcept.

Returns:



28
29
30
# File 'lib/glossarist/managed_concept.rb', line 28

def localizations
  @localizations
end

#localized_conceptsObject

return [Array<LocalizedConcept>]



22
23
24
# File 'lib/glossarist/managed_concept.rb', line 22

def localized_concepts
  @localized_concepts
end

Returns:



13
14
15
# File 'lib/glossarist/managed_concept.rb', line 13

def related
  @related
end

Instance Method Details

#add_localization(localized_concept) ⇒ Object Also known as: add_l10n

Adds concept localization.

Parameters:



58
59
60
61
# File 'lib/glossarist/managed_concept.rb', line 58

def add_localization(localized_concept)
  lang = localized_concept.language_code
  localizations.store(lang, localized_concept)
end

#default_definitionObject



88
89
90
91
# File 'lib/glossarist/managed_concept.rb', line 88

def default_definition
  localized = localization("eng") || localizations.values.first
  localized&.definition&.first&.content
end

#default_designationObject



83
84
85
86
# File 'lib/glossarist/managed_concept.rb', line 83

def default_designation
  localized = localization("eng") || localizations.values.first
  localized&.terms&.first&.designation
end

#default_langObject



93
94
95
# File 'lib/glossarist/managed_concept.rb', line 93

def default_lang
  localization("eng") || localizations.values.first
end

#localization(lang) ⇒ LocalizedConcept Also known as: l10n

Returns concept localization.

Parameters:

  • lang (String)

    language code

Returns:



68
69
70
# File 'lib/glossarist/managed_concept.rb', line 68

def localization(lang)
  localizations[lang]
end

#managed_concept_attributesObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/glossarist/managed_concept.rb', line 97

def managed_concept_attributes
  %i[
    id
    termid
    related
    status
    dates
    localized_concepts
  ].compact
end

#statusString

Returns:

  • (String)


16
# File 'lib/glossarist/managed_concept.rb', line 16

register_enum :status, Glossarist::GlossaryDefinition::CONCEPT_STATUSES

#to_hObject



74
75
76
77
78
79
80
81
# File 'lib/glossarist/managed_concept.rb', line 74

def to_h
  {
    "termid" => id,
    "term" => default_designation,
    "related" => related&.map(&:to_h),
    "dates" => dates&.empty? ? nil : dates&.map(&:to_h),
  }.merge(localizations.transform_values(&:to_h)).compact
end