Skip to content

Commit

Permalink
Some urgent changes to OnRecognizer for CommonBlockAcceptor to work w…
Browse files Browse the repository at this point in the history
…ith new way of Responders doing things
  • Loading branch information
Ryan Neufeld authored and Ryan Neufeld committed Apr 10, 2010
1 parent c8e5697 commit 656fe75
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Botfly Todo List (by priority)
==============================
* Have Bot.new and Bot#run instead of just #login that always runs
* Write cucumber integration testes
* Replace CommonBlockAcceptor responder chain array with ResponderChain object
** Spec & create ResponderChain class
** Have responder chain stop call first responder ONLY after reaching a match
Expand Down
16 changes: 15 additions & 1 deletion lib/botfly/common_block_acceptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,26 @@ class OnRecognizer
def initialize(obj); @obj = obj; end

def method_missing(name,&block)
@type = name
Botfly.logger.info("#{@obj.to_debug_s}: Bot#on")
klass = Botfly.const_get(@obj.class_prefix + name.to_s.split('_').map(&:capitalize).join + "Responder")

(@obj.responders[name] ||= []) << responder = klass.new(@obj, &block)
Botfly.logger.info("#{@obj.to_debug_s}: #{@obj.class_prefix}#{name.to_s.capitalize}Responder added to responder chain")
return responder
end

private
def klass
begin
klass = Botfly.const_get(@obj.class_prefix + @type.to_s.split('_').map(&:capitalize).join + "Responder")
rescue
if @obj.class_prefix.empty?
klass = Responder
else
klass = Botfly.const_get(@obj.class_prefix+"Responder")
end
end
end
end

def class_prefix; ''; end
Expand Down
15 changes: 13 additions & 2 deletions spec/botfly/common_block_acceptor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FooResponder; def initialize(bar); end; end
class FooBarResponder; def initialize(baz);end; end
end

it "should instanciate responder based on name" do
it "should instanciate specialized responder based on name if class exists" do
FooResponder.should_receive(:new)
on.foo
end
Expand All @@ -65,7 +65,7 @@ class FooBarResponder; def initialize(baz);end; end
it "should add class prefix to front of responder class" do
acceptor.stub(:class_prefix).and_return('Foo')
Botfly.should_receive(:const_get).with('FooBarResponder').and_return(FooBarResponder)
on.bar
on.bar.should be_a_kind_of FooBarResponder
end

it "should add responder to object's responder chain hash" do
Expand All @@ -80,6 +80,17 @@ class FooBarResponder; def initialize(baz);end; end
FooResponder.stub(:new).and_return(:foo_instance)
on.foo.should be :foo_instance
end

it "should default to Responder for basic responder" do
Responder.stub(:new).and_return(:plain)
on.baz.should be :plain
end

it "should still add class prefix even if special responder not found" do
acceptor.stub(:class_prefix).and_return('Foo')
FooResponder.stub(:new).and_return(:plain)
on.baz.should be :plain
end
end
end

0 comments on commit 656fe75

Please sign in to comment.