Instantly share code, notes, and snippets.

View pizza_store_v2.rb
class PizzaStore
attr_accessor :pizza_prototype_collection
def initialize
@pizza_prototype_collection = {}
end
def take_order(pizza_type)
pizza_prototype = pizza_prototype_collection[pizza_type]
raise 'unsupported pizza type' unless pizza_prototype
View pizza_store_with_mocha.rb
class PizzaStore
def take_order(pizza_type)
case pizza_type
when 'matcha'
pizza = Pizza.new('Matcha Pizza', ['matcha powder'])
when 'pepperoni'
pizza = Pizza.new('Pepperoni Pizza', ['pepperoni', 'shredded mozzarella cheese'])
when 'chicken'
pizza = Pizza.new('Chicken Pizza', ['chicken', 'mushroom', 'spinach'])
else
View pizza_store.rb
class PizzaStore
def take_order(pizza_type)
case pizza_type
when 'pepperoni'
pizza = Pizza.new('Pepperoni Pizza', ['pepperoni', 'shredded mozzarella cheese'])
when 'chicken'
pizza = Pizza.new('Chicken Pizza', ['chicken', 'mushroom', 'spinach'])
else
pizza = Pizza.new('Cheese Pizza', ['cheese'])
end
View pizza.rb
class Pizza
attr_reader :name, :toppings, :state
def initialize(name, toppings)
@name = name
@toppings = toppings
@state = 'raw'
end
def bake
View car_construction_director.rb
class CarConstructionDirector
def construct_car(builder:)
builder.build_car_frame
builder.add_engine
builder.add_front_wheels
builder.add_back_wheels
builder.add_dashboard
builder.add_energy_source
builder.car
end
View electronic_car_builder_v2.rb
class ElectronicCarBuilder
attr_reader :car
def initialize
@car = ''
end
def build_car_frame
car << "This is an electronic car\n"
end
View sports_car_builder_v2.rb
class SportsCarBuilder
attr_reader :car
def initialize
@car = ''
end
def build_car_frame
car << "This is a sport car\n"
end
View standard_car_builder_v2.rb
class StandardCarBuilder
attr_reader :car
def initialize
@car = ''
end
def build_car_frame
car << "This is a standard car\n"
end
View electronic_car_builder.rb
class ElectronicCarBuilder
def build
car = ''
# 1. build car frame
car << "This is an electronic car\n"
# 2. add an engine
car << " with a electronic engine\n"
# 3. add front wheels
car << " with two front wheels\n"
# 4. add back wheels
View sports_car_builder.rb
class SportsCarBuilder
def build
car = ''
# 1. build car frame
car << "This is a sport car\n"
# 2. add an engine
car << " with a powerful engine\n"
# 3. add front wheels
car << " with two front wheels suitable for mountain paths\n"
# 4. add back wheels