Module: ISO3166::CountryClassMethods

Included in:
Country
Defined in:
lib/countries/country/class_methods.rb

Constant Summary collapse

FIND_BY_REGEX =
/^find_(all_)?(country_|countries_)?by_(.+)/

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/countries/country/class_methods.rb', line 57

def method_missing(method_name, *arguments)
  matches = method_name.to_s.match(FIND_BY_REGEX)
  return_all = matches[1]
  super unless matches

  countries = find_by(matches[3], arguments[0], matches[2])
  return_all ? countries : countries.last
end

Instance Method Details

#[](query) ⇒ Object



53
54
55
# File 'lib/countries/country/class_methods.rb', line 53

def [](query)
  search(query)
end

#all(&blk) ⇒ Object Also known as: countries



26
27
28
29
# File 'lib/countries/country/class_methods.rb', line 26

def all(&blk)
  blk ||= proc { |_alpha2, d| ISO3166::Country.new(d) }
  ISO3166::Data.cache.map(&blk)
end

#all_names_with_codes(locale = 'en') ⇒ Object



37
38
39
40
41
42
# File 'lib/countries/country/class_methods.rb', line 37

def all_names_with_codes(locale = 'en')
  Country.all.map do |c|
    lc = (c.translation(locale) || c.name)
    [lc.respond_to?('html_safe') ? lc.html_safe : lc, c.alpha2]
  end.sort
end

#all_translated(locale = 'en') ⇒ Object



33
34
35
# File 'lib/countries/country/class_methods.rb', line 33

def all_translated(locale = 'en')
  translations(locale).values
end

#codesObject



22
23
24
# File 'lib/countries/country/class_methods.rb', line 22

def codes
  ISO3166::Data.codes
end

#find_all_by(attribute, val) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/countries/country/class_methods.rb', line 75

def find_all_by(attribute, val)
  attributes, lookup_value = parse_attributes(attribute, val)

  ISO3166::Data.cache.select do |_, v|
    country = Country.new(v)
    attributes.any? do |attr|
      Array(country.send(attr)).any? { |n| lookup_value === strip_accents(n) }
    end
  end
end

#new(country_data) ⇒ Object



18
19
20
# File 'lib/countries/country/class_methods.rb', line 18

def new(country_data)
  super if country_data.is_a?(Hash) || codes.include?(country_data.to_s.upcase)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
# File 'lib/countries/country/class_methods.rb', line 66

def respond_to_missing?(method_name, include_private = false)
  matches = method_name.to_s.match(FIND_BY_REGEX)
  if matches && matches[3]
    matches[3].all? { |a| instance_methods.include?(a.to_sym) }
  else
    super
  end
end

#search(query) ⇒ Object



48
49
50
51
# File 'lib/countries/country/class_methods.rb', line 48

def search(query)
  country = new(query.to_s.upcase)
  country && country.valid? ? country : nil
end

#strip_accents(v) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/countries/country/class_methods.rb', line 86

def strip_accents(v)
  if v.is_a?(Regexp)
    Regexp.new(v.source.unaccent, 'i')
  else
    UnicodeUtils.downcase(v.to_s.unaccent)
  end
end

#translations(locale = 'en') ⇒ Object



44
45
46
# File 'lib/countries/country/class_methods.rb', line 44

def translations(locale = 'en')
  I18nData.countries(locale.upcase)
end