Module: WorldFlags::Util::Country
- Defined in:
- lib/world_flags/util/country.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #countries ⇒ Object
- #countries=(countries) ⇒ Object
- #country(code = :us, locale = :en) ⇒ Object
- #country_label(code, locale) ⇒ Object
- #find_country_map(loc) ⇒ Object
Instance Attribute Details
#countries_map ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/world_flags/util/country.rb', line 35 def countries_map @countries_map ||= begin available_locales.inject({}) do |res, loc| res[loc] = find_country_map(loc) res end end end |
#hashied_countries ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/world_flags/util/country.rb', line 44 def hashied_countries @hashied_countries ||= begin case countries when Hash Hashie::Mash.new countries else countries end end end |
Instance Method Details
#countries ⇒ Object
31 32 33 |
# File 'lib/world_flags/util/country.rb', line 31 def countries @countries ||= Countries.new end |
#countries=(countries) ⇒ Object
26 27 28 29 |
# File 'lib/world_flags/util/country.rb', line 26 def countries= countries raise ArgumentError, "Must be a hash, was: #{countries}" unless !countries || countries.kind_of?(Hash) @countries = countries end |
#country(code = :us, locale = :en) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/world_flags/util/country.rb', line 10 def country code = :us, locale = :en locale ||= default_locale_used locale = WorldFlags.locale(locale).to_sym locale_countries_map = countries_map[locale] || countries_map[default_locale_used] locale_countries_map = countries_map[:en] if locale_countries_map.blank? raise "No country-locale map defined for locale: #{locale} or :en in #{countries.inspect}" if locale_countries_map.blank? # raise("No country map defined for country code: #{code} in #{locale_countries_map[code]}") locale_countries_map[code] ? locale_countries_map[code] : locale_countries_map[default_code_used] rescue Exception => e raise e if WorldFlags.raise_error? "Undefined" end |
#country_label(code, locale) ⇒ Object
6 7 8 |
# File 'lib/world_flags/util/country.rb', line 6 def country_label code, locale WorldFlags.country code, locale end |
#find_country_map(loc) ⇒ Object
55 56 57 58 59 |
# File 'lib/world_flags/util/country.rb', line 55 def find_country_map loc hashied_countries.respond_to?(loc) ? hashied_countries.send(loc) : hashied_countries.send(locale loc) rescue hashied_countries.send(default_locale_used) end |