Module: WorldFlags::Util::Country

Extended by:
NameEnabler
Defined in:
lib/world_flags/util/country.rb

Instance Attribute Summary collapse

Attributes included from NameEnabler

#name_enabled

Instance Method Summary collapse

Methods included from NameEnabler

name_disable!, name_enable!, name_enabled?

Instance Attribute Details

#countries_mapObject



39
40
41
42
43
44
45
46
# File 'lib/world_flags/util/country.rb', line 39

def countries_map
  @countries_map ||= begin 
    available_locales.inject({}) do |res, loc|
      res[loc] = find_country_map(loc)
      res
    end
  end
end

#hashied_countriesObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/world_flags/util/country.rb', line 48

def hashied_countries
  @hashied_countries ||= begin
    case countries
    when Hash
      Hashie::Mash.new countries
    else
      countries
    end
  end
end

Instance Method Details

#countriesObject



35
36
37
# File 'lib/world_flags/util/country.rb', line 35

def countries
  @countries ||= Countries.new
end

#countries=(countries) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
# File 'lib/world_flags/util/country.rb', line 30

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/world_flags/util/country.rb', line 14

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



10
11
12
# File 'lib/world_flags/util/country.rb', line 10

def country_label code, locale
  WorldFlags.country code, locale
end

#find_country_map(loc) ⇒ Object



59
60
61
62
63
# File 'lib/world_flags/util/country.rb', line 59

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