Skip to content

Instantly share code, notes, and snippets.

@vysogot
Created July 16, 2011 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vysogot/1086731 to your computer and use it in GitHub Desktop.
Save vysogot/1086731 to your computer and use it in GitHub Desktop.
FilterMatcher module
module FilterMatcher
#
# define filter in a class that uses this module
# named like:
# - name_filter
# - age_filter
#
# they should a filtered array
#
def filter_and_match(collection, filters)
filters.each do |key, param|
method_name = key.to_s.tr('by_', '') + "_filter"
filtered = send(method_name.to_sym, collection, param)
collection = filtered.empty? ? collection : filtered
if collection.size == 1
element_matched(collection.first)
break
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment