Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/car_production_line.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/chassis_state.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/engine_state.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/hood_state.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/illigal_jump_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class IlligalJumpError < StandardError
end
9 changes: 9 additions & 0 deletions lib/wheels_state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class WheelsState
def initialize(context)
@context = context
end

def next(part_to_install)
raise NotImplementedError
end
end
6 changes: 3 additions & 3 deletions spec/anything_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down