Module: CountriesAndLanguages

Extended by:
CountriesAndLanguages
Included in:
CountriesAndLanguages
Defined in:
lib/countries_and_languages.rb,
lib/countries_and_languages/version.rb

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

CONVERSIONS =
[
  ['áä', 'a'],
  ['ÁÄÅ', 'A'],
  ['óö', 'o'],
  ['ÓÖ', 'O'],
  ['í', 'i'],
  ['Í', 'I'],
  ['úü', 'u'],
  ['ÚÜ', 'U'],
  ['é', 'e'],
  ['É', 'E'],
  ['ß', 's'],
]
RUBY_18 =
RUBY_VERSION < "1.9"
VERSION =
Version = '0.1.6'

Instance Method Summary collapse

Instance Method Details

#clean_and_sort(data) ⇒ Object



27
28
29
30
31
# File 'lib/countries_and_languages.rb', line 27

def clean_and_sort(data)
  data = data.to_a
  data.map!{|code,name| [clean_name(name), code] }
  data.sort_by{|code,name| convert_umlaut_to_base(code) }
end

#clean_name(name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/countries_and_languages.rb', line 33

def clean_name(name)
  #General fixes
  name = name.sub(/\s*[,;(].*/,'')

  #German fixes
  name.sub!(/-Sprache$/,'')
  name.sub!(/ Peoples Democratic Republics Democratic Republic/,'')#Lao
  name.sub!(/Demokratische Republik /,'')#Congo

  name
end

#convert_umlaut_to_base(input) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/countries_and_languages.rb', line 61

def convert_umlaut_to_base(input)
  input = input.dup
  if RUBY_18
    old = $KCODE
    $KCODE='u'
    CONVERSIONS.each { |from, to| input.gsub!(/[#{from}]/, to) }
    $KCODE = old
  else
    CONVERSIONS.each { |from, to| input.tr!(from, to) }
  end
  input
end