Skip to content

Instantly share code, notes, and snippets.

@tracyloisel
Last active June 13, 2017 15:30
Show Gist options
  • Save tracyloisel/659f3f02d07f97121e64b488cce8e99d to your computer and use it in GitHub Desktop.
Save tracyloisel/659f3f02d07f97121e64b488cce8e99d to your computer and use it in GitHub Desktop.
Write chainable methods at class level
#PizzaHut.pepperoni(6).
# italian_sausage_ml(20).
# ham(6).
# bacon(6).
# seasoned_pork_and_beef(10).
# deliver_to(street: "Infinite loop", city: "Cupertino", name: "Tim Cook").
# go!
module PizzaHutClassMethods
def current_pizza(recipes_params = {})
@__pizza ||= PizzaBuilder.new(recipes_params)
end
def pepperoni(qty)
current_pizza.add(pepperony: qty)
self
end
def ham(qty)
current_pizza.add(ham: qty)
self
end
def bacon(qty)
current_pizza.add(bacon: qty)
self
end
def seasoned_pork_and_beef(qty)
current_pizza.add(seasoned_pork_and_beef: qty)
self
end
end
class PizzaHut
extend PizzaHutClassMethods
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment