Created
November 24, 2016 15:51
-
-
Save Martin-Nyaga/49cf889815665528622f93ca6521aa89 to your computer and use it in GitHub Desktop.
This file contains 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 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