Skip to content

Instantly share code, notes, and snippets.

@Martin-Nyaga
Created November 24, 2016 15:51
Show Gist options
  • Save Martin-Nyaga/49cf889815665528622f93ca6521aa89 to your computer and use it in GitHub Desktop.
Save Martin-Nyaga/49cf889815665528622f93ca6521aa89 to your computer and use it in GitHub Desktop.
class TicketFactory
def self.ticket_for opts
class_for(opts[:vehicle_type]).new(opts)
end
# Method to auto generate class from a vehicle_type
# e.g "Private car" => PrivateCarParkingTicket
# "Matatu" => MatatuParkingTicket
def self.class_for vehicle_type
prefix = vehicle_type.underscore.parameterize("_").camelize
"#{prefix}ParkingTicket".constantize
# If the class was not found
rescue NameError
ParkingTicket
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment