Skip to content

Instantly share code, notes, and snippets.

@lchanmann
Last active June 1, 2018 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lchanmann/7b36d75fab177e010a823a8421c3bd35 to your computer and use it in GitHub Desktop.
Save lchanmann/7b36d75fab177e010a823a8421c3bd35 to your computer and use it in GitHub Desktop.
bob_bot_2
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