Class: Osgeo::Termbase::Concept

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

Constant Summary collapse

DEFAULT_LANGUAGE =
"eng"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Concept



10
11
12
13
14
15
16
17
18
19
# File 'lib/osgeo/termbase/concept.rb', line 10

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

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

Instance Attribute Details

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/osgeo/termbase/concept.rb', line 6

def id
  @id
end

#termsObject

Returns the value of attribute terms.



7
8
9
# File 'lib/osgeo/termbase/concept.rb', line 7

def terms
  @terms
end

Instance Method Details

#add_term(term) ⇒ Object



27
28
29
# File 'lib/osgeo/termbase/concept.rb', line 27

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

#default_termObject



31
32
33
34
35
36
37
38
# File 'lib/osgeo/termbase/concept.rb', line 31

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

#to_file(filename) ⇒ Object



51
52
53
54
55
# File 'lib/osgeo/termbase/concept.rb', line 51

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

#to_hashObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/osgeo/termbase/concept.rb', line 40

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

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