Skip to content

Instantly share code, notes, and snippets.

@pumpkincouture
Last active August 29, 2015 14:21
Show Gist options
  • Save pumpkincouture/d9bd56255f15ebd3e187 to your computer and use it in GitHub Desktop.
Save pumpkincouture/d9bd56255f15ebd3e187 to your computer and use it in GitHub Desktop.
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