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_(.+)/
SEARCH_TERM_FILTER_REGEX =
/\(|\)|\[\]|,/

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments) ⇒ Object



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

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



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

def [](query)
  search(query)
end

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



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

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

#all_names_with_codes(locale = 'en') ⇒ Object



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

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



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

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

#codesObject



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

def codes
  ISO3166::Data.codes
end

#find_all_by(attribute, val) ⇒ Object



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

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 === parse_value(n) }
    end
  end
end

#new(country_data) ⇒ Object



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

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)


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

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



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

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

#translations(locale = 'en') ⇒ Object



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

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