lib/world_flags/util/country.rb in world-flags-0.4.2 vs lib/world_flags/util/country.rb in world-flags-0.4.3

- old
+ new

@@ -1,18 +1,14 @@ module WorldFlags module Util module Country - attr_writer :countries_map + attr_writer :countries_map, :hashied_countries def country_label code, locale WorldFlags.country code, locale end - def countries - @countries ||= Countries.new - end - 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] @@ -30,22 +26,37 @@ def countries= countries raise ArgumentError, "Must be a hash, was: #{countries}" unless !countries || countries.kind_of?(Hash) @countries = countries end + def countries + @countries ||= Countries.new + end + def countries_map @countries_map ||= begin available_locales.inject({}) do |res, loc| res[loc] = find_country_map(loc) res end end end - def find_country_map loc - countries.respond_to?(loc) ? countries.send(loc) : languages.send(locale(loc)) + def hashied_countries + @hashied_countries ||= begin + case countries + when Hash + Hashie::Mash.new countries + else + countries + end + end + end + + def find_country_map loc + hashied_countries.respond_to?(loc) ? hashied_countries.send(loc) : hashied_countries.send(locale loc) rescue - countries.send(default_locale_used) + hashied_countries.send(default_locale_used) end end end end \ No newline at end of file