Skip to content

Commit

Permalink
Minor refactor to increase readability of method missing
Browse files Browse the repository at this point in the history
  • Loading branch information
rposborne committed Sep 20, 2016
1 parent 44bb45d commit efc729f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 29 deletions.
19 changes: 1 addition & 18 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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'
35 changes: 24 additions & 11 deletions lib/countries/country/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit efc729f

Please sign in to comment.