Skip to content

Commit

Permalink
+ visitor & visitors
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed May 9, 2011
1 parent df7cf11 commit c5ab0e2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
8 changes: 4 additions & 4 deletions lib/james/visitor.rb
Expand Up @@ -21,13 +21,13 @@ def initialize initial, timer = nil
@current = initial

@initial = initial
@timer = timer || Timer.new
# @timer = timer || Timer.new
end

# Escapes the current state back to the initial.
#
def escape
timer.stop
def reset
# timer.stop
self.current = initial
end

Expand All @@ -53,7 +53,7 @@ def check
end
def hear phrase, &block
return unless hears? phrase
timer.restart
# timer.restart
exit_text = exit &block
transition phrase
check &block
Expand Down
17 changes: 16 additions & 1 deletion lib/james/visitors.rb
Expand Up @@ -22,12 +22,27 @@ def initialize *visitors
@visitors = visitors
end

# Hear tries all visitors in order
# until one hears a phrase he knows.
#
# After that, all remaining visitors are reset.
#
def hear phrase, &block
visitors.each do |visitor|
iterator = visitors.each

while visitor = iterator.next
visitor.hear phrase, &block and break
end

while visitor = iterator.next
visitor.reset
end
rescue StopIteration
# That's ok! Nothing to do here.
end

# Simply returns the sum of what phrases all dialogues expect.
#
def expects
visitors.inject([]) { |expects, visitor| expects + visitor.expects }
end
Expand Down
35 changes: 25 additions & 10 deletions spec/lib/james/visitor_spec.rb
Expand Up @@ -8,6 +8,30 @@
let(:timer) { stub :timer }
let(:visitor) { described_class.new initial, timer }

describe 'reset' do
it 'works' do
expect { visitor.reset }.to_not raise_error
end
# it 'calls methods in order' do
# timer.should_receive(:stop).once.with
# visitor.should_receive(:current=).once.with initial
#
# visitor.reset
# end
it 'survives a functional test' do
next_state = stub :next_state
initial.stub! :next_for => next_state

visitor.current.should == initial
visitor.transition :some_phrase
visitor.current.should == next_state

visitor.reset

visitor.current.should == initial
end
end

describe 'current' do
it { visitor.current.should == initial }
end
Expand Down Expand Up @@ -62,19 +86,10 @@
end
end

describe 'escape' do
it 'calls methods in order' do
timer.should_receive(:stop).once.with
visitor.should_receive(:current=).once.with initial

visitor.escape
end
end

describe 'hear' do
it 'calls methods in order' do
visitor.should_receive(:hears?).once.ordered.with(:some_phrase).and_return true
timer.should_receive(:restart).once.ordered.with
# timer.should_receive(:restart).once.ordered.with
visitor.should_receive(:exit).once.ordered.with
visitor.should_receive(:transition).once.ordered.with :some_phrase
# visitor.should_receive(:check).once.ordered.with
Expand Down
3 changes: 3 additions & 0 deletions spec/lib/james/visitors_spec.rb
Expand Up @@ -15,11 +15,14 @@
second.stub! :hear => nil
end
it 'works' do
second.stub! :reset

visitors.hear 'some phrase'
end
it 'calls the second never' do
first.should_receive(:hear).once.and_return true
second.should_receive(:hear).never
second.should_receive(:reset).once

visitors.hear 'some phrase'
end
Expand Down

0 comments on commit c5ab0e2

Please sign in to comment.