Created
November 19, 2011 01:31
-
-
Save mike-burns/1378284 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
kitchen = SafeGrillDecorator.new( | |
:kitchen => ExposingDecorator.new( | |
:expose => [:strawberry_fluff,:peanut_butter, :bread, :knife], | |
:kitchen => UnsafeBareKitchen.new)) | |
sandwich_maker = SandwichMaker.new | |
possible_sandwich = sandwich_maker. | |
collect_knife(kitchen). | |
with_that(:collect_bread). | |
with_that(:collect_strawberry_fluff). | |
with_that(:collect_peanut_butter). | |
with_that(:spread_strawberry_fluff). | |
with_that(:grill) | |
decider = EitherDecider.new(possible_sandwich) | |
puts decider.on_failure(&:message).on_success(&:payload) | |
class UnsafeBareKitchenKitchen | |
def initialize(values = {}) | |
@exposing = values[:exposing] || [] | |
@ingredients = values[:ingredients] || [] | |
end | |
def exposing(item) | |
Failure.new("We don't got #{item} 'round here.") | |
end | |
def merge_ingredients(first, second) | |
new_ingredients = @ingredients-[first,second]+["#{first} and #{second}"] | |
Success.new(cloning(:ingredients => new_ingredients)) | |
end | |
def grill | |
raise "Kitchen caught fire." | |
end | |
def to_s | |
@ingredients.inspect | |
end | |
def cloning(additions = {}) | |
new_params = { | |
:exposing => @exposing, | |
:ingredients => @ingredients | |
}.merge(additions)) | |
UnsafeBareKitchenKitchen.new(new_params) | |
end | |
end | |
class ExposingDecorator | |
def initialize(values = {}) | |
@kitchen = values[:kitchen].cloning(values) | |
@to_expose = values[:expose] | |
@exposing = values[:exposing] | |
end | |
def exposing(item) | |
if @to_expose.include?(item) | |
Success.new(cloning(:exposing => @exposing+[item])) | |
else | |
@kitchen.exposing(item) | |
end | |
end | |
def cloning(additions = {}) | |
new_kitchen = @kitchen.cloning(additions) | |
new_params = {:kitchen => new_kitchen}.merge(additions) | |
ExposingDecorator.new(new_params) | |
end | |
end | |
class SafeGrillDecorator | |
def initialize(values = {}) | |
@kitchen = values[:kitchen].cloning(values) | |
@ingredients = values[:ingredients] | |
end | |
def grill | |
new_ingredients = @ingredients.map{|i| "grilled #{i}"} | |
Success.new(cloning(:ingredients => new_ingredients)) | |
end | |
def cloning(additions = {}) | |
new_kitchen = @kitchen.cloning(additions) | |
new_params = {:kitchen => new_kitchen}.merge(additions) | |
SafeGrillDecorator.new(new_params) | |
end | |
end | |
class SandwichMaker | |
def collect_knife(kitchen) | |
kitchen.exposing(:knife) | |
end | |
def collect_bread(kitchen) | |
kitchen.exposing(:bread) | |
end | |
def collect_strawberry_fluff(kitchen) | |
kitchen.exposing(:strawberry_fluff) | |
end | |
def collect_peanut_butter(kitchen) | |
kitchen.exposing(:peanut_butter) | |
end | |
def spread_strawberry_fluff(kitchen) | |
kitchen.merge_ingredients(:bread, :strawberry_fluff) | |
end | |
def grill(kitchen) | |
ExceptionWrapper.new { kitchen.grill(:bread_strawberry_fluff) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment