Class: Glossarist::Concept

Inherits:
Model
  • Object
show all
Defined in:
lib/glossarist/concept.rb

Direct Known Subclasses

LocalizedConcept

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

new, #set_attribute, #snake_case

Constructor Details

#initialize(*args) ⇒ Concept

Returns a new instance of Concept.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/glossarist/concept.rb', line 52

def initialize(*args)
  @localizations = {}
  @sources = Glossarist::Collections::Collection.new(klass: ConceptSource)
  @related = Glossarist::Collections::Collection.new(klass: RelatedConcept)
  @definition = Glossarist::Collections::Collection.new(klass: DetailedDefinition)
  @notes = Glossarist::Collections::Collection.new(klass: DetailedDefinition)
  @examples = Glossarist::Collections::Collection.new(klass: DetailedDefinition)
  @dates = Glossarist::Collections::Collection.new(klass: ConceptDate)

  @designations = Glossarist::Collections::DesignationCollection.new
  @extension_attributes = {}

  normalize_args(args)

  super
end

Instance Attribute Details

#datesObject

return [Array<ConceptDate>]



96
97
98
# File 'lib/glossarist/concept.rb', line 96

def dates
  @dates
end

#definitionArray<DetailedDefinition>

Concept definition.

Returns:



30
31
32
# File 'lib/glossarist/concept.rb', line 30

def definition
  @definition
end

#designationsArray<Designations::Base> Also known as: terms

TODO:

Alias terms exists only for legacy reasons and will be removed.

Concept designations.

Returns:

  • (Array<Designations::Base>)


17
18
19
# File 'lib/glossarist/concept.rb', line 17

def designations
  @designations
end

#domainString

<<BasicDocument>>LocalizedString

Returns:

  • (String)


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

def domain
  @domain
end

#examplesArray<DetailedDefinition>

Concept examples

Returns:



42
43
44
# File 'lib/glossarist/concept.rb', line 42

def examples
  @examples
end

#extension_attributesObject

Contains list of extended attributes



45
46
47
# File 'lib/glossarist/concept.rb', line 45

def extension_attributes
  @extension_attributes
end

#idString

Concept ID.

Returns:

  • (String)


10
11
12
# File 'lib/glossarist/concept.rb', line 10

def id
  @id
end

#lineage_sourceObject

Returns the value of attribute lineage_source.



47
48
49
# File 'lib/glossarist/concept.rb', line 47

def lineage_source
  @lineage_source
end

#lineage_source_similarityObject

Returns the value of attribute lineage_source_similarity.



48
49
50
# File 'lib/glossarist/concept.rb', line 48

def lineage_source_similarity
  @lineage_source_similarity
end

#non_verb_repNonVerbRep

Non verbal representation of the concept.

Returns:



34
35
36
# File 'lib/glossarist/concept.rb', line 34

def non_verb_rep
  @non_verb_rep
end

#notesArray<DetailedDefinition>

Concept notes

Returns:



38
39
40
# File 'lib/glossarist/concept.rb', line 38

def notes
  @notes
end

#releaseObject

Returns the value of attribute release.



50
51
52
# File 'lib/glossarist/concept.rb', line 50

def release
  @release
end

#sourcesArray<ConceptSource>

TODO:

Alias authoritative_source exists for legacy reasons and may be removed.

List of authorative sources.

Returns:



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

def sources
  @sources
end

#subjectString

<<BasicDocument>>LocalizedString

Returns:

  • (String)


26
27
28
# File 'lib/glossarist/concept.rb', line 26

def subject
  @subject
end

#uuidObject



69
70
71
72
73
74
# File 'lib/glossarist/concept.rb', line 69

def uuid
  @uuid ||= Glossarist::Utilities::UUID.uuid_v5(
    Glossarist::Utilities::UUID::OID_NAMESPACE,
    to_h_no_uuid.to_yaml,
  )
end

Class Method Details

.from_h(hash) ⇒ Object

rubocop:disable Metrics/AbcSize, Style/RescueModifier



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/glossarist/concept.rb', line 191

def self.from_h(hash)
  new.tap do |concept|
    concept.id = hash.dig("termid") || hash.dig("id")
    concept.sources = hash.dig("sources")
    concept.related = hash.dig("related")
    concept.definition = hash.dig("definition")

    hash.values
      .grep(Hash)
      .map { |subhash| Config.class_for(:localized_concept).from_h(subhash) rescue nil }
      .compact

    concept.related = hash.dig("related") || []
  end
end

Instance Method Details

#authoritative_sourceObject



136
137
138
# File 'lib/glossarist/concept.rb', line 136

def authoritative_source
  @sources.select { |source| source.authoritative? }
end

#authoritative_source=(sources) ⇒ Object



140
141
142
143
144
# File 'lib/glossarist/concept.rb', line 140

def authoritative_source=(sources)
  sources&.each do |source|
    @sources << source.merge({ "type" => "authoritative" })
  end
end

#date_acceptedObject



156
157
158
159
# File 'lib/glossarist/concept.rb', line 156

def date_accepted
  return nil unless @dates
  @dates.find { |date| date.accepted? }
end

#date_accepted=(date) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/glossarist/concept.rb', line 146

def date_accepted=(date)
  date_hash = {
    "type" => "accepted",
    "date" => date,
  }

  @dates ||= []
  @dates << ConceptDate.new(date_hash)
end

#normalize_args(args) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/glossarist/concept.rb', line 231

def normalize_args(args)
  args.each do |arg|
    data = arg.delete("data")

    arg.merge!(data) if data

    if arg["sources"]
      arg.delete("authoritative_source")
      arg.delete("authoritativeSource")
    end
  end
end

#preferred_designationsObject Also known as: preferred_terms



120
121
122
# File 'lib/glossarist/concept.rb', line 120

def preferred_designations
  @designations.select(&:preferred?)
end

All Related Concepts

Returns:



210
211
212
# File 'lib/glossarist/concept.rb', line 210

def related
  @related.empty? ? nil : @related
end

#related=(related) ⇒ Object



214
215
216
217
# File 'lib/glossarist/concept.rb', line 214

def related=(related)
  @related.clear!
  related&.each { |r| @related << r }
end

#to_hObject Also known as: to_hash



182
183
184
# File 'lib/glossarist/concept.rb', line 182

def to_h
  to_h_no_uuid.merge("id" => uuid)
end

#to_h_no_uuidObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/glossarist/concept.rb', line 161

def to_h_no_uuid
  {
    "data" => {
      "dates" => dates&.map(&:to_h),
      "definition" => definition&.map(&:to_h),
      "examples" => examples&.map(&:to_h),
      "id" => id,
      "lineage_source_similarity" => lineage_source_similarity,
      "notes" => notes&.map(&:to_h),
      "release" => release,
      "sources" => sources.empty? ? nil : sources&.map(&:to_h),
      "terms" => (terms&.map(&:to_h) || []),
      "related" => related&.map(&:to_h),
      "domain" => domain,
    }.compact,

    "date_accepted" => date_accepted&.date,

  }.compact
end