Last active
December 24, 2015 15:09
-
-
Save burtlo/6817579 to your computer and use it in GitHub Desktop.
Class Macros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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