Skip to content

Instantly share code, notes, and snippets.

@burtlo
Last active December 24, 2015 15:09
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 burtlo/6817579 to your computer and use it in GitHub Desktop.
Save burtlo/6817579 to your computer and use it in GitHub Desktop.
Class Macros
module Finders
def find_by(*attributes)
attributes.each do |attribute|
define_method("find_by_#{attribute}") do |criteria|
puts "Finding by #{attribute} with #{criteria}"
all.find{|c| c.send(attribute).to_s == criteria.to_s}
end
end
end
end
class MerchantRepository
# include or extend
# include it means you want instance methods
# ^ ^
# extend makes class methods
extend Finders
find_by :id, :name, :created_at, :updated_at
def all
[]
end
end
class CustomerRepository
extend Finders
find_by :id, :first_name, :last_name, :created_at, :updated_at
def all
[]
end
end
repo = CustomerRepository.new
repo.find_by_id("1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment