Module: ISO3166::CountryFinderMethods
- Included in:
- Country
- Defined in:
- lib/countries/country/finder_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
30
31
32
33
34
35
36
37
|
# File 'lib/countries/country/finder_methods.rb', line 30
def method_missing(method_name, *arguments)
matches = method_name.to_s.match(FIND_BY_REGEX)
super unless matches
return_all = matches[1]
countries = find_by(matches[3], arguments[0], matches[2])
return_all ? countries : countries.last
end
|
Instance Method Details
#[](query) ⇒ Object
13
14
15
|
# File 'lib/countries/country/finder_methods.rb', line 13
def [](query)
search(query)
end
|
#find_all_by(attribute, val) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/countries/country/finder_methods.rb', line 17
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
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
39
40
41
42
43
44
45
46
|
# File 'lib/countries/country/finder_methods.rb', line 39
def respond_to_missing?(method_name, include_private = false)
matches = method_name.to_s.match(FIND_BY_REGEX)
if matches && matches[3]
instance_methods.include?(matches[3].to_sym)
else
super
end
end
|
#search(query) ⇒ Object
8
9
10
11
|
# File 'lib/countries/country/finder_methods.rb', line 8
def search(query)
country = new(query.to_s.upcase)
country&.valid? ? country : nil
end
|