diff --git a/lib/car_production_line.rb b/lib/car_production_line.rb index e69de29..def08ad 100644 --- a/lib/car_production_line.rb +++ b/lib/car_production_line.rb @@ -0,0 +1,11 @@ +class CarProductionLine + attr_accessor :state + + def initialize + @state = ChassisState.new self + end + + def install(part_to_install) + @state.next(part_to_install) + end +end diff --git a/lib/chassis_state.rb b/lib/chassis_state.rb index e69de29..82a23db 100644 --- a/lib/chassis_state.rb +++ b/lib/chassis_state.rb @@ -0,0 +1,11 @@ +class ChassisState + def initialize(context) + @context = context + end + + def next(part_to_install) + raise IlligalJumpError.new("State machine isn't being respected") unless part_to_install == :engine + + @context.state = EngineState.new(@context) + end +end diff --git a/lib/engine_state.rb b/lib/engine_state.rb index e69de29..cd5930e 100644 --- a/lib/engine_state.rb +++ b/lib/engine_state.rb @@ -0,0 +1,11 @@ +class EngineState + def initialize(context) + @context = context + end + + def next(part_to_install) + raise IlligalJumpError.new("State machine isn't being respected") unless part_to_install == :hood + + @context.state = HoodState.new(@context) + end +end diff --git a/lib/hood_state.rb b/lib/hood_state.rb index e69de29..db2be8f 100644 --- a/lib/hood_state.rb +++ b/lib/hood_state.rb @@ -0,0 +1,11 @@ +class HoodState + def initialize(context) + @context = context + end + + def next(part_to_install) + raise IlligalJumpError.new("State machine isn't being respected") unless part_to_install == :wheels + + @context.state = WheelsState.new(@context) + end +end diff --git a/lib/illigal_jump_error.rb b/lib/illigal_jump_error.rb index e69de29..1b9805b 100644 --- a/lib/illigal_jump_error.rb +++ b/lib/illigal_jump_error.rb @@ -0,0 +1,2 @@ +class IlligalJumpError < StandardError +end diff --git a/lib/wheels_state.rb b/lib/wheels_state.rb index e69de29..f604fe4 100644 --- a/lib/wheels_state.rb +++ b/lib/wheels_state.rb @@ -0,0 +1,9 @@ +class WheelsState + def initialize(context) + @context = context + end + + def next(part_to_install) + raise NotImplementedError + end +end diff --git a/spec/anything_spec.rb b/spec/anything_spec.rb index cb03550..bd6592e 100644 --- a/spec/anything_spec.rb +++ b/spec/anything_spec.rb @@ -13,20 +13,20 @@ let(:line) { CarProductionLine.new } it '1st step - chassis' do - expect{ line.state }.to be(ChassisState) + expect(line.state).to be_a(ChassisState) end it '2nd step - enging' do line.install :engine - expect{ line.state }.to be(EngineState) + expect(line.state).to be_a(EngineState) end it '3rd step - hood' do line.install :engine line.install :hood - expect{ line.state }.to be(HoodState) + expect(line.state).to be_a(HoodState) end it '3rd step - hood' do