-
-
Save radar/26a95a7e9b6dfc2558af6e2b6dea27bd 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 Question | |
end | |
class Answer | |
attr_reader :question, :score | |
def initialize(question, score) | |
@question = question | |
@score = score | |
end | |
end | |
module AnswerScoreHelpers | |
refine Answer do | |
def low? | |
score < 3 | |
end | |
def neutral? | |
score == 3 | |
end | |
def high? | |
score > 3 | |
end | |
end | |
end | |
class Response | |
using AnswerScoreHelpers | |
attr_reader :answers | |
def initialize(answers = []) | |
@answers = answers | |
end | |
def low_answers_for_question(question) | |
answers.select(&for_question(question)).select(&:low?) | |
end | |
def for_question(question) | |
->(answer) { answer.question == question } | |
end | |
end | |
question = Question.new | |
answer_1 = Answer.new(question, 1) | |
answer_2 = Answer.new(question, 2) | |
answer_3 = Answer.new(question, 3) | |
answer_4 = Answer.new(question, 4) | |
answer_5 = Answer.new(question, 5) | |
response = Response.new([answer_1, answer_2, answer_3, answer_4, answer_5]) | |
p response.low_answers_for_question(question) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment