Skip to content

Commit

Permalink
compartmentalisation of cukesalad objects into a cukesalad module
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed May 6, 2011
1 parent a7ff7ef commit 156c115
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 247 deletions.
76 changes: 39 additions & 37 deletions lib/cukesalad/actor.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
require 'cukesalad/director'

class Actor

def initialize this_type_of_role, directed_by=Director.new
@director = directed_by
get_into_character_for this_type_of_role
@note_pad = {}
end

def perform described_task, details = {}
get_ready_to_perform described_task
@info = details
perform_task
end
alias :answer :perform
module CukeSalad
class Actor

def initialize this_type_of_role, directed_by=Director.new
@director = directed_by
get_into_character_for this_type_of_role
@note_pad = {}
end

def perform described_task, details = {}
get_ready_to_perform described_task
@info = details
perform_task
end
alias :answer :perform

def get_into_character_for described_role
the_role = @director.explain_the_role described_role
see_how_to_do the_role
end

def get_ready_to_perform something
the_thing = @director.how_do_i_perform something
see_how_to_do the_thing
end
def get_into_character_for described_role
the_role = @director.explain_the_role described_role
see_how_to_do the_role
end
def get_ready_to_perform something
the_thing = @director.how_do_i_perform something
see_how_to_do the_thing
end

def see_how_to_do something
extend something
end
def see_how_to_do something
extend something
end

def value_of(symbol)
@info[symbol]
end
def value_of(symbol)
@info[symbol]
end

def take_note_of key, value
@note_pad.store key, value
end
def take_note_of key, value
@note_pad.store key, value
end

def recall key
begin
@note_pad.fetch key
rescue KeyError
raise KeyError, "You tried to recall ':#{key}' but no previous step appears to have taken note of that information."
def recall key
begin
@note_pad.fetch key
rescue KeyError
raise KeyError, "You tried to recall ':#{key}' but no previous step appears to have taken note of that information."
end
end
end
end
36 changes: 19 additions & 17 deletions lib/cukesalad/codify/const_name.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
module Codify
module ConstName
def ConstName.from sentence
joined_together capitalised( words_from sentence )
end

private
def ConstName.joined_together words
words.join
end

def ConstName.words_from this_sentence
on_word_boundary = /\s|([A-Z][a-z]+)/
this_sentence.split( on_word_boundary )
end
module CukeSalad
module Codify
module ConstName
def ConstName.from sentence
joined_together capitalised( words_from sentence )
end

private
def ConstName.joined_together words
words.join
end

def ConstName.words_from this_sentence
on_word_boundary = /\s|([A-Z][a-z]+)/
this_sentence.split( on_word_boundary )
end

def ConstName.capitalised words
words.collect{ | word | word.capitalize }
def ConstName.capitalised words
words.collect{ | word | word.capitalize }
end
end
end
end
42 changes: 22 additions & 20 deletions lib/cukesalad/director.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
require 'cukesalad/codify/const_name'
class Director
module CukeSalad
class Director

include Codify

#TODO: Needs refactoring
def explain_the_role description
name = ConstName.from description
begin
find_directives_for name
rescue NameError
raise "I can't find a role called '#{ name }'. Have you created it?\ne.g.\n module #{ name }\n end"
include Codify

#TODO: Needs refactoring
def explain_the_role description
name = ConstName.from description
begin
find_directives_for name
rescue NameError
raise "I can't find a role called '#{ name }'. Have you created it?\ne.g.\n module #{ name }\n end"
end
end
end

def how_do_i_perform something
name = ConstName.from something
begin
find_directives_for name
rescue NameError
raise "I can't find a task called '#{ something }'. Have you created it?\ne.g.\n in_order_to '#{ something }' do\n # the actions\n end"
def how_do_i_perform something
name = ConstName.from something
begin
find_directives_for name
rescue NameError
raise "I can't find a task called '#{ something }'. Have you created it?\ne.g.\n in_order_to '#{ something }' do\n # the actions\n end"
end
end
end

def find_directives_for something
Kernel.const_get( something )
def find_directives_for something
Kernel.const_get( something )
end
end
end
54 changes: 28 additions & 26 deletions lib/cukesalad/specifics.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
module Specifics
def understand_the details
@info = with_specifics_from( details )
end

def value_of(symbol)
@info[symbol]
end
module CukeSalad
module Specifics
def understand_the details
@info = with_specifics_from( details )
end

def value_of(symbol)
@info[symbol]
end

def with_specifics_from details
result = {}
names_and_values_in(details).each_slice(2) do |name_value|
result[symbolized name_value[0]] = the_value_from_the name_value[1]
def with_specifics_from details
result = {}
names_and_values_in(details).each_slice(2) do |name_value|
result[symbolized name_value[0]] = the_value_from_the name_value[1]
end
result
end
result
end

def set_last value
@info[@info.keys.last] = value
end
def set_last value
@info[@info.keys.last] = value
end

def names_and_values_in details
specifics_pattern = /('[^']+')/
details.split(specifics_pattern)
end
def names_and_values_in details
specifics_pattern = /('[^']+')/
details.split(specifics_pattern)
end

def symbolized name
name.strip.gsub(' ', '_').to_sym
end
def symbolized name
name.strip.gsub(' ', '_').to_sym
end

def the_value_from_the item
item.gsub(/(^'|'$)/, '') unless item.nil?
def the_value_from_the item
item.gsub(/(^'|'$)/, '') unless item.nil?
end
end
end
2 changes: 1 addition & 1 deletion lib/cukesalad/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Cukesalad
module CukeSalad
VERSION = "0.6.0"
end
125 changes: 63 additions & 62 deletions spec/cukesalad/actor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,67 +29,68 @@ def place_the_knife_on_the_right_of place

end

describe Actor do

it "gets into character" do
role_description = "Butler"
director = double("director")
director.should_receive( :explain_the_role ).with( role_description ).and_return( Butler )

actor = Actor.new role_description, director
end

it "performs a simple task as that character" do
role_description = "Butler"

task_description = "set the place at the table"

director = double("director")
director.should_receive( :explain_the_role ).with( role_description ).and_return( Butler )
director.should_receive( :how_do_i_perform ).with( task_description ).and_return( SetThePlaceAtTheTable )

actor = Actor.new role_description, director

actor.perform(task_description).should == ["fork","knife"]
end

it "can perform a task that requires certain details" do
role_description = "Butler"

task_description = "Lay The Table"
details = { with_places_for: '5' }

director = double("director")
director.should_receive( :explain_the_role ).with( role_description ).and_return( Butler )
director.should_receive( :how_do_i_perform ).with( task_description ).and_return( LayTheTable )

actor = Actor.new role_description, director

actor.perform( task_description, details ).should == 5
end

it "can take note of information" do
role_description = "Butler"

director = double("director")
director.should_receive( :explain_the_role ).with( role_description ).and_return( Butler )

actor = Actor.new role_description, director

actor.take_note_of :something, "of importance"
actor.recall( :something ).should == "of importance"
end

it "tells you when it can't find the information you're looking for" do
role_description = "Butler"

director = double("director")
director.should_receive( :explain_the_role ).with( role_description ).and_return( Butler )

actor = Actor.new role_description, director

lambda {actor.recall( :something )}.should raise_error KeyError,
"You tried to recall ':something' but no previous step appears to have taken note of that information."
module CukeSalad
describe Actor do

it "gets into character" do
role_description = "Butler"
director = double("director")
director.should_receive( :explain_the_role ).with( role_description ).and_return( Butler )

actor = Actor.new role_description, director
end

it "performs a simple task as that character" do
role_description = "Butler"

task_description = "set the place at the table"

director = double("director")
director.should_receive( :explain_the_role ).with( role_description ).and_return( Butler )
director.should_receive( :how_do_i_perform ).with( task_description ).and_return( SetThePlaceAtTheTable )

actor = Actor.new role_description, director

actor.perform(task_description).should == ["fork","knife"]
end

it "can perform a task that requires certain details" do
role_description = "Butler"

task_description = "Lay The Table"
details = { with_places_for: '5' }

director = double("director")
director.should_receive( :explain_the_role ).with( role_description ).and_return( Butler )
director.should_receive( :how_do_i_perform ).with( task_description ).and_return( LayTheTable )

actor = Actor.new role_description, director

actor.perform( task_description, details ).should == 5
end

it "can take note of information" do
role_description = "Butler"

director = double("director")
director.should_receive( :explain_the_role ).with( role_description ).and_return( Butler )

actor = Actor.new role_description, director

actor.take_note_of :something, "of importance"
actor.recall( :something ).should == "of importance"
end

it "tells you when it can't find the information you're looking for" do
role_description = "Butler"

director = double("director")
director.should_receive( :explain_the_role ).with( role_description ).and_return( Butler )

actor = Actor.new role_description, director

lambda {actor.recall( :something )}.should raise_error KeyError,
"You tried to recall ':something' but no previous step appears to have taken note of that information."
end
end
end

Loading

0 comments on commit 156c115

Please sign in to comment.