Class: IEV::Iso639Code

Inherits:
Object
  • Object
show all
Defined in:
lib/iev/iso_639_code.rb

Overview

TODO:

This needs to be rewritten.

Constant Summary collapse

COUNTRY_CODES =
YAML.load(IO.read(File.join(__dir__, "iso_639_2.yaml")))
THREE_CHAR_MEMO =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(two_char_code) ⇒ Iso639Code

Returns a new instance of Iso639Code.



12
13
14
15
16
17
18
19
20
# File 'lib/iev/iso_639_code.rb', line 12

def initialize(two_char_code)
  @code = case two_char_code.length
  when 2
    two_char_code
  else
    # This is to handle code "nl BE" in the IEV sheet
    two_char_code.split(" ").first
  end
end

Class Method Details

.three_char_code(two_char_code, code_type = "terminology") ⇒ Object



34
35
36
37
# File 'lib/iev/iso_639_code.rb', line 34

def self.three_char_code(two_char_code, code_type = "terminology")
  memo_index = [two_char_code, code_type]
  THREE_CHAR_MEMO[memo_index] ||= new(two_char_code).find(code_type)
end

Instance Method Details

#find(code_type) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/iev/iso_639_code.rb', line 22

def find(code_type)
  code = country_codes.detect do |key, value|
    key if value["iso_639_1"] == @code.to_s && value[code_type]
  end

  if code.nil?
    raise StandardError.new("Iso639Code not found for '#{@code}'!")
  end

  code
end