Skip to content

Instantly share code, notes, and snippets.

@tsuka tsuka/my_dynamic_finders.rb Secret
Last active Aug 27, 2019

Embed
What would you like to do?
module MyDynamicFinders
def method_missing(sym, *args)
name = sym.to_s
if name =~ /\Afind_by_(\w+)(!)?\Z/
params = $1.split("_and_")
bang = $2
record = $DATABASE.find do |user|
params.zip(args).all? do |attr, value|
user.__send__(attr.to_sym) == value
end
end
if bang && record.nil?
raise
end
record
else
super
end
end
def respond_to_missing?(sym, include_private)
sym.to_s =~ /\Afind_by_\w+(!)?\Z/
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.