Last active
August 29, 2015 14:21
-
-
Save pumpkincouture/d9bd56255f15ebd3e187 to your computer and use it in GitHub Desktop.
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
class PatronsRepository | |
def initialize(db) | |
@db = db | |
end | |
def create!(attrs={}) | |
build_model db.patrons.create!(attrs) | |
end | |
def update!(attrs={}) | |
build_model db.patrons.update!(patron_type, id, attrs) | |
end | |
def find_by_id(patron_type, id) | |
build_model db.patrons.find_by_id(patron_type, id) | |
end | |
private | |
attr_reader :db | |
def build_model(row) | |
if row | |
case row[:patron_type] | |
when :adult | |
AdultPatron.new(row) | |
when :child | |
ChildPatron.new(row) | |
when :senior | |
SeniorPatron.new(row) | |
else | |
raise ArgumentError.new("Unknown user type: #{row[:patron_type]}") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment