Last active
August 2, 2019 11:17
-
-
Save nafidurmus/ff45b2e4527210edb560585b006f32b9 to your computer and use it in GitHub Desktop.
This file contains 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 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