Created
March 18, 2011 11:35
-
-
Save RiverGlide/875919 to your computer and use it in GitHub Desktop.
Beginnings of interesting dynamic role switching example
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
$:.unshift(File.dirname(__FILE__), ".") | |
require 'rubygems' | |
require 'rspec' | |
module Butler | |
def self.extended base | |
base.get_ready | |
end | |
def get_ready | |
@place = [] | |
end | |
def put_fork_on_the_left | |
@place.unshift "fork" | |
@place | |
end | |
def put_knife_on_the_right | |
@place.push "knife" | |
@place | |
end | |
end | |
module Cook | |
#Do something similar here | |
def boil_water | |
end | |
def turn_on_stove | |
end | |
def place_boil_in_bag_into_pot | |
end | |
end | |
module MakeDinner | |
def follow_instructions | |
turn_on_stove | |
boil_water | |
place_boil_in_bag_into_pot | |
end | |
end | |
module LayTable | |
def follow_instructions | |
put_fork_on_the_left | |
put_knife_on_the_right | |
end | |
end | |
class MyActor | |
#decide how we will make the actor know in which role they should perform the task | |
def initialize role | |
extend role | |
end | |
def perform task | |
recall_how_to_do task | |
follow_instructions | |
end | |
private | |
def recall_how_to_do something | |
extend something | |
end | |
end | |
describe "Something" do | |
it "Something" do | |
butler = MyActor.new Butler | |
butler.perform(LayTable).should == ["fork","knife"] | |
end | |
end |
See this forked gist for a fix to the @place
instance variable problem.
Almost... except self.included should be self.extended
see this forked gist
I've found another approach to solving the original problem
see this gist
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created during pairing session with @antonymarcano in London, UK and JamesMartin (@jmrtn ) in Sydney, Australia