diff --git a/lib/cukesalad/actor.rb b/lib/cukesalad/actor.rb index 11f77c3..703e4a6 100644 --- a/lib/cukesalad/actor.rb +++ b/lib/cukesalad/actor.rb @@ -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 diff --git a/lib/cukesalad/codify/const_name.rb b/lib/cukesalad/codify/const_name.rb index 7a4707f..b92707b 100644 --- a/lib/cukesalad/codify/const_name.rb +++ b/lib/cukesalad/codify/const_name.rb @@ -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 diff --git a/lib/cukesalad/director.rb b/lib/cukesalad/director.rb index b5f59a7..9baa4f4 100644 --- a/lib/cukesalad/director.rb +++ b/lib/cukesalad/director.rb @@ -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 diff --git a/lib/cukesalad/specifics.rb b/lib/cukesalad/specifics.rb index 95aaced..297b834 100644 --- a/lib/cukesalad/specifics.rb +++ b/lib/cukesalad/specifics.rb @@ -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 diff --git a/lib/cukesalad/version.rb b/lib/cukesalad/version.rb index 38caee8..6733c0b 100644 --- a/lib/cukesalad/version.rb +++ b/lib/cukesalad/version.rb @@ -1,3 +1,3 @@ -module Cukesalad +module CukeSalad VERSION = "0.6.0" end diff --git a/spec/cukesalad/actor_spec.rb b/spec/cukesalad/actor_spec.rb index 92a86ff..f73d73b 100644 --- a/spec/cukesalad/actor_spec.rb +++ b/spec/cukesalad/actor_spec.rb @@ -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 - diff --git a/spec/cukesalad/codify/as_const_name_spec.rb b/spec/cukesalad/codify/as_const_name_spec.rb index c793c9f..bb61c64 100644 --- a/spec/cukesalad/codify/as_const_name_spec.rb +++ b/spec/cukesalad/codify/as_const_name_spec.rb @@ -1,27 +1,30 @@ require 'spec_helper' require 'cukesalad/codify/const_name' -describe Codify::ConstName do - include Codify - - it "gives you ClassName from when it's already a class name" do - ConstName.from( "ClassName" ).should == "ClassName" - end +module CukeSalad + module Codify + describe ConstName do + + it "gives you ClassName from when it's already a class name" do + ConstName.from( "ClassName" ).should == "ClassName" + end - it "gives you ClassName when separated by spaces" do - ConstName.from( "Class Name" ).should == "ClassName" - end + it "gives you ClassName when separated by spaces" do + ConstName.from( "Class Name" ).should == "ClassName" + end - it "gives you ClassName from lowercase class name" do - ConstName.from( "class name" ).should == "ClassName" - end + it "gives you ClassName from lowercase class name" do + ConstName.from( "class name" ).should == "ClassName" + end + + it "gives you ClassName from lower case class name with leading/trailing spaces" do + ConstName.from( " class name " ).should == "ClassName" + end + + it "formats as a ClassName from a mix of class format and lower case" do + ConstName.from( "AnotherClass name " ).should == "AnotherClassName" + end + end - it "gives you ClassName from lower case class name with leading/trailing spaces" do - ConstName.from( " class name " ).should == "ClassName" - end - - it "formats as a ClassName from a mix of class format and lower case" do - ConstName.from( "AnotherClass name " ).should == "AnotherClassName" end end - diff --git a/spec/cukesalad/director_spec.rb b/spec/cukesalad/director_spec.rb index 8392f5f..688ff8c 100644 --- a/spec/cukesalad/director_spec.rb +++ b/spec/cukesalad/director_spec.rb @@ -7,27 +7,30 @@ class Another class MoreThanOneWord end -describe Director do - before :each do - @director = Director.new - end +module CukeSalad + describe Director do - it "gives you directives (i.e. a class) for Something" do - the_thing_we_found = @director.how_do_i_perform "something" - the_thing_we_found.should == Something - end + before :each do + @director = Director.new + end - it "finds directives (i.e. a class) for something described with more than one word" do - the_thing_we_found = @director.how_do_i_perform "more than one word" - the_thing_we_found.should == MoreThanOneWord - end + it "gives you directives (i.e. a class) for Something" do + the_thing_we_found = @director.how_do_i_perform "something" + the_thing_we_found.should == Something + end - it "apologises when it can't find the role" do - lambda { @director.explain_the_role "non existent role" }.should raise_error RuntimeError - end + it "finds directives (i.e. a class) for something described with more than one word" do + the_thing_we_found = @director.how_do_i_perform "more than one word" + the_thing_we_found.should == MoreThanOneWord + end + + it "apologises when it can't find the role" do + lambda { @director.explain_the_role "non existent role" }.should raise_error RuntimeError + end - it "apologises when it can't find the task" do - lambda { @director.how_do_i_perform "non existent task" }.should raise_error RuntimeError + it "apologises when it can't find the task" do + lambda { @director.how_do_i_perform "non existent task" }.should raise_error RuntimeError + end end end diff --git a/spec/cukesalad/specifics_spec.rb b/spec/cukesalad/specifics_spec.rb index f1e4070..5be1675 100644 --- a/spec/cukesalad/specifics_spec.rb +++ b/spec/cukesalad/specifics_spec.rb @@ -1,66 +1,68 @@ require 'spec_helper' class NeedingSpecifics - include Specifics + include CukeSalad::Specifics end -describe Specifics do +module CukeSalad + describe Specifics do - it "has an item of specific information" do - something = NeedingSpecifics.new - something.understand_the "specific 'information'" - something.value_of(:specific).should == "information" - end - - it "has items of specific information" do - something = NeedingSpecifics.new - something.understand_the "first 'item' second 'another'" - something.value_of(:first).should == "item" - something.value_of(:second).should == "another" - end - - it "copes with names having more than one word" do - something = NeedingSpecifics.new - something.understand_the "first thing 'item' second thing 'another'" - something.value_of(:first_thing).should == "item" - something.value_of(:second_thing).should == "another" - end - - it "should cope with values having more than one word" do - something = NeedingSpecifics.new - something.understand_the "first thing 'item' second thing 'another thing'" - something.value_of(:first_thing).should == "item" - something.value_of(:second_thing).should == "another thing" - end - - context 'the last item' do - it "can be empty" do + it "has an item of specific information" do something = NeedingSpecifics.new - something.understand_the "containing" - something.value_of(:containing).should be_nil + something.understand_the "specific 'information'" + something.value_of(:specific).should == "information" end - it "can be the only item and have a value assigned" do + it "has items of specific information" do something = NeedingSpecifics.new - something.understand_the "containing" - something.set_last('some value') - something.value_of(:containing).should == 'some value' + something.understand_the "first 'item' second 'another'" + something.value_of(:first).should == "item" + something.value_of(:second).should == "another" end - it "can be the only item and be empty" do - something = NeedingSpecifics.new - something.understand_the "called 'something' containing" - something.value_of(:called).should == 'something' - something.value_of(:containing).should be_nil + it "copes with names having more than one word" do + something = NeedingSpecifics.new + something.understand_the "first thing 'item' second thing 'another'" + something.value_of(:first_thing).should == "item" + something.value_of(:second_thing).should == "another" end - - it "can have a value assigned" do + + it "should cope with values having more than one word" do something = NeedingSpecifics.new - something.understand_the "called 'something' containing" - something.value_of(:called).should == 'something' - something.set_last('some value') - something.value_of(:containing).should == 'some value' + something.understand_the "first thing 'item' second thing 'another thing'" + something.value_of(:first_thing).should == "item" + something.value_of(:second_thing).should == "another thing" end + context 'the last item' do + it "can be empty" do + something = NeedingSpecifics.new + something.understand_the "containing" + something.value_of(:containing).should be_nil + end + + it "can be the only item and have a value assigned" do + something = NeedingSpecifics.new + something.understand_the "containing" + something.set_last('some value') + something.value_of(:containing).should == 'some value' + end + + it "can be the only item and be empty" do + something = NeedingSpecifics.new + something.understand_the "called 'something' containing" + something.value_of(:called).should == 'something' + something.value_of(:containing).should be_nil + end + + it "can have a value assigned" do + something = NeedingSpecifics.new + something.understand_the "called 'something' containing" + something.value_of(:called).should == 'something' + something.set_last('some value') + something.value_of(:containing).should == 'some value' + end + + end end end