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
54
55
56
57
58
59
60
61
|
# File 'lib/countries/country/class_methods.rb', line 54
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
50
51
52
|
# File 'lib/countries/country/class_methods.rb', line 50
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
|
# File 'lib/countries/country/class_methods.rb', line 37
def all_names_with_codes(locale = 'en')
Country.all.map { |c| [(c.translation(locale) || c.name).html_safe, c.alpha2] }.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
|
#codes ⇒ Object
22
23
24
|
# File 'lib/countries/country/class_methods.rb', line 22
def codes
ISO3166::Data.codes
end
|
#find_all_by(attribute, val) ⇒ Object
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/countries/country/class_methods.rb', line 72
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
63
64
65
66
67
68
69
70
|
# File 'lib/countries/country/class_methods.rb', line 63
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
45
46
47
48
|
# File 'lib/countries/country/class_methods.rb', line 45
def search(query)
country = new(query.to_s.upcase)
country && country.valid? ? country : nil
end
|
#strip_accents(v) ⇒ Object
83
84
85
86
87
88
89
|
# File 'lib/countries/country/class_methods.rb', line 83
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
41
42
43
|
# File 'lib/countries/country/class_methods.rb', line 41
def translations(locale = 'en')
I18nData.countries(locale.upcase)
end
|