diff --git a/.rubocop.yml b/.rubocop.yml index df165e7c0..18b0dda5c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,18 +1 @@ -# This configuration was generated by -# `rubocop --auto-gen-config` -# on 2016-09-19 23:23:44 -0400 using RuboCop version 0.43.0. -# The point is for the user to remove these configuration records -# one by one as the offenses are removed from the code base. -# Note that changes in the inspected code, or installation of new -# versions of RuboCop, may require this file to be generated again. - -# Offense count: 1 -# Configuration parameters: CountComments. -Metrics/ClassLength: - Max: 101 - -# Offense count: 64 -# Configuration parameters: AllowHeredoc, AllowURI, URISchemes. -# URISchemes: http, https -Metrics/LineLength: - Max: 483 +inherit_from: .rubocop_todo.yml \ No newline at end of file diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 000000000..a73bdc203 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,52 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2016-09-19 23:29:07 -0400 using RuboCop version 0.43.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. +# Offense count: 1 +# Configuration parameters: CountComments. +Metrics/ClassLength: + Max: 101 + +# Offense count: 64 +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes. +# URISchemes: http, https +Metrics/LineLength: + Max: 483 + +# Offense count: 1 +Metrics/AbcSize: + Max: 19 + +# Offense count: 1 +Style/CaseEquality: + Exclude: + - 'lib/countries/country/class_methods.rb' + +# Offense count: 7 +Style/ClassVars: + Exclude: + - 'lib/countries/data.rb' + +# Offense count: 9 +Style/Documentation: + Exclude: + - 'spec/**/*' + - 'test/**/*' + - 'lib/countries/configuration.rb' + - 'lib/countries/country.rb' + - 'lib/countries/country/class_methods.rb' + - 'lib/countries/country/emoji.rb' + - 'lib/countries/global.rb' + - 'lib/countries/kwarg_struct.rb' + - 'lib/countries/mongoid.rb' + + +# Offense count: 1 +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: snake_case, camelCase +Style/MethodName: + Exclude: + - 'lib/countries/country/class_methods.rb' diff --git a/lib/countries/country/class_methods.rb b/lib/countries/country/class_methods.rb index 7d2959be0..4913bdc75 100644 --- a/lib/countries/country/class_methods.rb +++ b/lib/countries/country/class_methods.rb @@ -11,6 +11,8 @@ def self::Country(country_data_or_country) end module CountryClassMethods + FIND_BY_REGEX = /^find_(all_)?(country_|countries_)?by_(.+)/ + def new(country_data) super if country_data.is_a?(Hash) || codes.include?(country_data.to_s.upcase) end @@ -47,25 +49,36 @@ def [](query) search(query) end - def method_missing(*m) - regex = m.first.to_s.match(/^find_(all_)?(country_|countries_)?by_(.+)/) - super unless regex + 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 - countries = find_by(Regexp.last_match[3], m[1], Regexp.last_match[2]) - Regexp.last_match[1] ? countries : countries.last + 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 def find_all_by(attribute, val) - attributes, value = parse_attributes(attribute, val) + attributes, lookup_value = parse_attributes(attribute, val) ISO3166::Data.cache.select do |_, v| - attributes.map do |attr| - Array(Country.new(v).send(attr)).any? { |n| value === sterlize_value(n) } - end.include?(true) + country = Country.new(v) + attributes.any? do |attr| + Array(country.send(attr)).any? { |n| lookup_value === strip_accents(n) } + end end end - def sterlize_value(v) + def strip_accents(v) if v.is_a?(Regexp) Regexp.new(v.source.unaccent, 'i') else @@ -85,7 +98,7 @@ def parse_attributes(attribute, val) # attributes << 'translated_names' end - [attributes, sterlize_value(val)] + [attributes, strip_accents(val)] end def find_by(attribute, value, obj = nil)