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 |
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 | |
TICKET_CLASSES = { | |
'Truck' => TruckParkingTicket, | |
'Matatu' => MatatuParkingTicket, | |
'Public Bus' => PublicBusParkingTicket | |
} | |
def self.ticket_for opts | |
# .fetch takes a second argument as a default if the | |
# key is not found |
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
parking_ticket = TicketFactory.ticket_for(opts) | |
# where opts is the hash for options |
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 | |
def self.class_for vehicle_type | |
case vehicle_type | |
when 'Truck' | |
TruckParkingTicket | |
when 'Matatu' |
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 PublicBusParkingTicket < ParkingTicket | |
def total_cost | |
@eet_passed == true ? surcharge : super | |
end | |
end |
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 MatatuParkingTicket < ParkingTicket | |
def base_fee | |
1100 | |
end | |
def surcharge | |
weekdays.inlcude?(@day) ? super * 1.5 : super | |
end | |
private |
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 TruckParkingTicket < ParkingTicket | |
def base_fee | |
1200 | |
end | |
def surcharge | |
super * 1.5 | |
end | |
end |
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 ParkingTicket | |
def initialize(opts) | |
@day = opts[:day] || raise(":day is required") | |
@vehicle_type = opts[:vehicle_type] || raise(":vehicle_type is required") | |
@eet_passed = opts[:eet_passed] || raise(":eet_passed is required") | |
... | |
end | |
def base_fee | |
1000 |
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
def beat_up thing | |
... | |
thing.make_sound | |
... | |
end | |
# thing can be an Animal, or a Dog, and this code will work. |
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 Animal | |
def initialize(name) | |
@name = name | |
end | |
def make_sound | |
puts "No sound" | |
end | |
end |
NewerOlder