构建功能核心与测试实践
1. 功能核心构建
在构建测验系统时,我们需要实现一系列功能,包括添加模板、选择随机问题、移动模板、重置测验等。以下是详细的实现步骤和代码分析。
1.1 添加模板
通过 add_template 函数可以向测验中添加新模板。代码如下:
def add_template(quiz, fields) do
template = Template.new(fields)
templates =
update_in(
quiz.templates,
[template.category],
&add_to_list_or_nil(&1, template)
)
%{quiz | templates: templates}
end
defp add_to_list_or_nil(nil, template), do: [template]
defp add_to_list_or_nil(templates, template), do: [template | templates]
这个函数的工作流程如下:
1. 创建一个新模板。
2. 更新 quiz.templates 中对应类别的模板列表。
3. 返回更新后的测验。
我们可以通过以下方式使用这个函数:
Quiz.new(
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



