Search⌘ K

Building Our Quizzes

Explore how to construct quizzes in Elixir by leveraging quiz builder functions and question templates. Understand the process of merging default attributes with overrides to create flexible quiz structures. This lesson demonstrates building a quiz with multiple templates and testing its implementation to ensure functionality.

With the templates out of the way, the hard work is mostly done.

Building quizzes

We can focus on building quizzes now, which is nearly trivial at this point. We’ll add the following code to /test/support/quiz_builders.exs:

C++
def quiz_fields(overrides) do
Keyword.merge([title: "Simple Arithmetic"], overrides)
end
def build_quiz(quiz_overrides \\ []) do
quiz_overrides
|> quiz_fields
|> Quiz.new
end
def build_question(overrides \\ [ ]) do
overrides
|> template_fields
|> Template.new
|> Question.new
end
  • Our quiz_fields function returns some default attributes and merges in overrides.

  • Then all build_quiz has to do is take the overrides, pipe them into quiz_fields, and pipe that into Quiz.new.

Building a question is also easy. Since we need only a template to generate a ...