Skip to content

Instantly share code, notes, and snippets.

@8th-Light-Blog
Created August 11, 2011 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 8th-Light-Blog/1140509 to your computer and use it in GitHub Desktop.
Save 8th-Light-Blog/1140509 to your computer and use it in GitHub Desktop.
Ruby DSL Blocks
module Starbucks
def self.order(&block)
order = Order.new
order.instance_eval(&block)
return order.drinks
end
class Order
attr_reader :drinks
def initialize
@drinks = []
end
def short
@size = "small"
return self
end
def tall
@size = "medium"
return self
end
def grande
@size = "large"
return self
end
def venti
@size = "extra large"
return self
end
def coffee
@drink = "coffee"
build_drink
end
def decaf
@drink = "decaffeinated coffee"
build_drink
end
def half_caff
@drink = "regular and decaffeinated coffee mixed together"
build_drink
end
def americano
@drink = "espresso"
build_drink
end
def mocha
@drink = "espresso mixed with chocolate, milk, and topped with whipped cream"
build_drink
end
def cappuchino
@drink = "espresso mixed with milk and topped with steamed milk"
build_drink
end
def no_whip
@adjective = "without the whipped cream"
return self
end
def non_fat
@adjective = "with non-fat milk"
return self
end
def breve
@adjective = "with half and half"
return self
end
def extra_shot
@adjective = "with an extra shot of espresso"
return self
end
def soy
@adjective = "with soy milk"
return self
end
def extra_hot
@adjective = "with super heated milk"
return self
end
private
def build_drink
drink = "#{@size} cup of #{@drink}"
drink << " #{@adjective}" if @adjective
@drinks << drink
@size = @drink = @adjective = nil
end
end
end
require File.expand_path(File.dirname(__FILE__) + "/starbucks")
#### Specs ####
describe "Starbucks" do
it "should handle empty orders" do
Starbucks.order {}.should eql([])
end
it "should handle regular coffee with all the sizes" do
Starbucks.order do
short.coffee
tall.coffee
grande.coffee
venti.coffee
end.should eql(["small cup of coffee",
"medium cup of coffee",
"large cup of coffee",
"extra large cup of coffee"])
end
it "should deal with types of drinks" do
Starbucks.order do
short.decaf
short.half_caff
short.americano
short.mocha
short.cappuchino
end.should eql(["small cup of decaffeinated coffee",
"small cup of regular and decaffeinated coffee mixed together",
"small cup of espresso",
"small cup of espresso mixed with chocolate, milk, and topped with whipped cream",
"small cup of espresso mixed with milk and topped with steamed milk"])
end
it "should handle the adjectives" do
Starbucks.order do
short.no_whip.coffee
short.non_fat.coffee
short.breve.coffee
short.extra_shot.coffee
short.soy.coffee
short.extra_hot.coffee
end.should eql(["small cup of coffee without the whipped cream",
"small cup of coffee with non-fat milk",
"small cup of coffee with half and half",
"small cup of coffee with an extra shot of espresso",
"small cup of coffee with soy milk",
"small cup of coffee with super heated milk",])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment