Class: Tc211::Termbase::Concept

Inherits:
Hash
  • Object
show all
Defined in:
lib/tc211/termbase/concept.rb

Constant Summary collapse

DEFAULT_LANGUAGE =
"eng".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Concept

Returns a new instance of Concept.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/tc211/termbase/concept.rb', line 7

def initialize(options = {})
  super

  terms = options.delete(:terms) || []
  terms.each do |term|
    add_term(term)
  end

  options.each_pair do |k, v|
    send("#{k}=", v)
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/tc211/termbase/concept.rb', line 3

def id
  @id
end

Instance Method Details

#add_term(term) ⇒ Object



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

def add_term(term)
  self[term.language_code] = term
end

#default_termObject



34
35
36
37
38
39
40
41
42
# File 'lib/tc211/termbase/concept.rb', line 34

def default_term
  if self[DEFAULT_LANGUAGE]
    self[DEFAULT_LANGUAGE]
  else
    puts "[tc211-termbase] term (lang: #{keys.first}, ID: #{id}) is \
      missing a corresponding English term, probably needs updating."
    self[keys.first]
  end
end

#termsObject



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

def terms
  values
end

#to_file(filename) ⇒ Object



55
56
57
58
59
# File 'lib/tc211/termbase/concept.rb', line 55

def to_file(filename)
  File.open(filename, "w") do |file|
    file.write(to_hash.to_yaml)
  end
end

#to_glossarist_conceptObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tc211/termbase/concept.rb', line 61

def to_glossarist_concept
  concept = Tc211::Termbase::Glossarist::ManagedConcept.new(
    data: { id: id.to_s },
  )

  localized_concepts = []

  terms.map do |term|
    next if term.nil?

    localized_concepts << term.to_localized_concept_hash
  end

  concept.localized_concepts = localized_concepts

  concept
end

#to_hashObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/tc211/termbase/concept.rb', line 44

def to_hash
  default_hash = {
    "term" => default_term.term,
    "termid" => id,
  }

  inject(default_hash) do |acc, (lang, term)|
    acc.merge!(lang => term.to_hash)
  end
end