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
- #[](query) ⇒ Object
-
#find_all_by(attribute, val) ⇒ Object
:reek:NestedIterators.
-
#method_missing(method_name, *arguments) ⇒ Object
:reek:FeatureEnvy.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
:reek:BooleanParameter.
-
#search(query) ⇒ Object
:reek:FeatureEnvy.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments) ⇒ Object
:reek:FeatureEnvy
36 37 38 39 40 41 42 43 44 |
# File 'lib/countries/country/finder_methods.rb', line 36 def method_missing(method_name, *arguments) matches = method_name.to_s.match(FIND_BY_REGEX) super unless matches return_all = matches[1] method = matches[3] countries = find_by(method, arguments[0], matches[2]) return_all ? countries : countries.last end |
Instance Method Details
#[](query) ⇒ Object
17 18 19 |
# File 'lib/countries/country/finder_methods.rb', line 17 def [](query) search(query) end |
#find_all_by(attribute, val) ⇒ Object
:reek:NestedIterators
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/countries/country/finder_methods.rb', line 22 def find_all_by(attribute, val) attributes, lookup_value = parse_attributes(attribute, val) ISO3166::Data.cache.select do |_k, value| country = Country.new(value) attributes.any? do |attr| Array(country.send(attr)).any? do |attr_value| lookup_value === cached(attr_value) { parse_value(attr_value) } end end end end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
:reek:BooleanParameter
47 48 49 50 51 52 53 54 |
# File 'lib/countries/country/finder_methods.rb', line 47 def respond_to_missing?(method_name, include_private = false) matches = method_name.to_s.match(FIND_BY_REGEX) return super unless matches && matches[3] method = matches[3] instance_methods.include?(method.to_sym) end |
#search(query) ⇒ Object
:reek:FeatureEnvy
9 10 11 12 13 14 15 |
# File 'lib/countries/country/finder_methods.rb', line 9 def search(query) query = query.to_s if query.is_a?(Symbol) query = query.upcase if query&.match?(/[a-z]/) country = new(query) country&.valid? ? country : nil end |