Last active
June 1, 2018 21:39
-
-
Save lchanmann/7b36d75fab177e010a823a8421c3bd35 to your computer and use it in GitHub Desktop.
bob_bot_2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Visitor | |
METHODS_TOBE_IMPLEMENTED = %i( | |
respond_to_question respond_to_yell respond_to_silence respond_to_anything) | |
attr_reader :message_types | |
def initialize(*message_types) | |
@message_types = message_types | |
end | |
def self.default | |
# NOTE: the message types will be processed in order | |
@bot ||= new(Question, Yell, Silence, Anything) | |
end | |
def respond(text) | |
message_types.each do |type| | |
message = type.new(text) | |
return message.accept(self) if message.valid? | |
end | |
raise "Can't respond to: #{text || 'nil'}" | |
end | |
def method_missing(method, *args, &block) | |
raise 'Not yet implemented' if METHODS_TOBE_IMPLEMENTED.include?(method) | |
super(method, *args, &block) | |
end | |
def respond_to?(method, *args, &block) | |
return true if METHODS_TOBE_IMPLEMENTED.include?(method) | |
super(method, *args, &block) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment