Skip to content

Instantly share code, notes, and snippets.

@nafidurmus
Last active August 2, 2019 11:17
Show Gist options
  • Save nafidurmus/ff45b2e4527210edb560585b006f32b9 to your computer and use it in GitHub Desktop.
Save nafidurmus/ff45b2e4527210edb560585b006f32b9 to your computer and use it in GitHub Desktop.
class QuestionHandler
attr_reader :successor
def initialize(successor = nil)
@successor = successor
end
def process_request(request)
if accept_request(request)
return
elsif @successor
@successor.process_request(request)
else
fail_request(request)
end
end
def fail_request(request)
puts "The question '#{request}' could not be answered."
end
def accept_request(request)
raise '#accept_request method must be implemented.'
end
end
class StarWarsQuestionHandler < QuestionHandler
def accept_request(request)
if request.include?("Star Wars")
answer_question(request)
return true
else
return false
end
end
def answer_question(request)
puts "answering a Star Wars related question: '#{request}'"
end
end
class HarryPotterQuestionHandler < QuestionHandler
def accept_request(request)
if request.include?("Harry Potter")
answer_question(request)
return true
else
return false
end
end
def answer_question(request)
puts "answering a Harry Potter related question: '#{request}'"
end
end
class LordOfTheRingsQuestionHandler < QuestionHandler
def accept_request(request)
if request.include?("Lord of the Rings")
answer_question(request)
return true
else
return false
end
end
def answer_question(request)
puts "answering a Lord of the Rings related question: '#{request}'"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment