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
61
62
63
64
65
66
67
68
|
# File 'lib/countries/country/class_methods.rb', line 61
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
57
58
59
|
# File 'lib/countries/country/class_methods.rb', line 57
def [](query)
search(query)
end
|
#all(&blk) ⇒ Object
Also known as:
countries
30
31
32
33
|
# File 'lib/countries/country/class_methods.rb', line 30
def all(&blk)
blk ||= proc { |_alpha2, d| ISO3166::Country.new(d) }
ISO3166::Data.cache.map(&blk)
end
|
#all_names_with_codes(locale = 'en') ⇒ Object
41
42
43
44
45
46
|
# File 'lib/countries/country/class_methods.rb', line 41
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
37
38
39
|
# File 'lib/countries/country/class_methods.rb', line 37
def all_translated(locale = 'en')
translations(locale).values
end
|
#codes ⇒ Object
26
27
28
|
# File 'lib/countries/country/class_methods.rb', line 26
def codes
ISO3166::Data.codes
end
|
#create_subdivisions(subdivision_data) ⇒ Object
97
98
99
100
101
|
# File 'lib/countries/country/class_methods.rb', line 97
def create_subdivisions(subdivision_data)
subdivision_data.each_with_object({}) do |(k, v), hash|
hash[k] = Subdivision.new(v)
end
end
|
#find_all_by(attribute, val) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/countries/country/class_methods.rb', line 79
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? do |n|
lookup_value === cached(n) { parse_value(n) }
end
end
end
end
|
#new(country_data) ⇒ Object
22
23
24
|
# File 'lib/countries/country/class_methods.rb', line 22
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
70
71
72
73
74
75
76
77
|
# File 'lib/countries/country/class_methods.rb', line 70
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
52
53
54
55
|
# File 'lib/countries/country/class_methods.rb', line 52
def search(query)
country = new(query.to_s.upcase)
country && country.valid? ? country : nil
end
|
#subdivisions(alpha2) ⇒ Object
92
93
94
95
|
# File 'lib/countries/country/class_methods.rb', line 92
def subdivisions(alpha2)
@subdivisions ||= {}
@subdivisions[alpha2] ||= create_subdivisions(subdivision_data(alpha2))
end
|
#translations(locale = 'en') ⇒ Object
48
49
50
|
# File 'lib/countries/country/class_methods.rb', line 48
def translations(locale = 'en')
I18nData.countries(locale.upcase)
end
|