Identifying the Data of Our Project

Let’s define the fields that will be present in the templates and questions of our quiz.

Creating our core

We’ll create a lib/mastery/core directory to hold the modules with our data layer (and later our core functions).

Defining a template

We’re going to use the primary Elixir data structure, the map. We know that the fields will be a struct. The centerpiece of our quiz is the template. The fields in our templates will serve three purposes—description, question generation, and checking responses.

Description

Our first three fields will describe our templates. As such, we’ll have a name and a category, which we’ll represent as atoms. We’ll also have an instruction to tell users what to do as they answer a question. These are the fields that describe our template:

  • name (atom): The name of this template.

  • category (atom): A grouping for questions of the same name.

  • instructions (string): A string telling the user how to answer questions of this type.

Question generation

Second, our templates will generate questions. We’ll need the raw and compiled version of the template to create a question and a generator for each substitution pattern in our template. These are the fields that support question generation:

  • raw (string): The template code before compilation.

  • compiled (macro): The compiled version of the template for execution.

  • generators (%{ substitution: list or function}): The generator for each substitution in a template. Each generator is a list of elements or a function. Generating a template substitution will either fire the function or pick a random item from the list.

Checking responses

Finally, our templates will check responses. This responsibility will fall on the checkers, which are functions. This is the field for processing responses:

  • checker (function(substitutions, string) -> boolean): Given the substitutions strings and an answer, the function returns true if the answer is correct. For example, fn subs, answer -> to_string(subs.left + subs.right) == String.trim(answer) end).

We’ll be creating a new file template.ex in the /lib/mastery/core directory:

Get hands-on with 1200+ tech skills courses.