Skip to content

Commit

Permalink
create global hashes for lookup for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
jrochkind committed Mar 8, 2013
1 parent 04fc125 commit 625bc2d
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/language_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ def to_s
end

def self.find_by_iso_639_1(code)
LanguageList::ISO_639_1.detect{|l| l.iso_639_1 == code }
LanguageList::BY_ISO_639_1[code]
end

def self.find_by_iso_639_3(code)
LanguageList::ALL_LANGUAGES.detect{|l| l.iso_639_3 == code }
LanguageList::BY_ISO_639_3[code]
end

def self.find_by_name(name)
LanguageList::ALL_LANGUAGES.detect{|l| l.name == name }
LanguageList::BY_NAME[name]
end

def self.find(code)
Expand All @@ -61,4 +61,14 @@ def self.find(code)
ISO_639_1 = ALL_LANGUAGES.select(&:iso_639_1?)
LIVING_LANGUAGES = ALL_LANGUAGES.select(&:living?)
COMMON_LANGUAGES = ALL_LANGUAGES.select(&:common?)

BY_NAME = {}
BY_ISO_639_1 = {}
BY_ISO_639_3 = {}
ALL_LANGUAGES.each do |lang|
BY_NAME[lang.name] = lang
BY_ISO_639_1[lang.iso_639_1] = lang if lang.iso_639_1
BY_ISO_639_3[lang.iso_639_3] = lang if lang.iso_639_3
end

end

0 comments on commit 625bc2d

Please sign in to comment.