Class: Kitchen::Directions::BakeInjectedExerciseQuestion::V1

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/directions/bake_injected_exercise/bake_injected_exercise_question.rb

Instance Method Summary collapse

Instance Method Details

#bake(question:, number:, only_number_solution:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kitchen/directions/bake_injected_exercise/bake_injected_exercise_question.rb', line 9

def bake(question:, number:, only_number_solution:)
  id = question.id

  # Store label in pantry
  unless only_number_solution
    label_number = "#{question.ancestor(:chapter).count_in(:book)}.#{number}"
    question.target_label(label_text: 'exercise', custom_content: label_number)
  end

  # Synthesize multiple choice solution
  if question.answers
    case question.answers[:type]
    when 'a'
      alphabet = *('a'..'z')
    else
      raise('Unsupported list type for multiple choice options')
    end
    letter_answers = question.correct_answer_letters(alphabet)
  end
  if letter_answers.present?
    question.append(child:
      <<~HTML
        <div data-type="question-solution">#{letter_answers.join(', ')}</div>
      HTML
    )
  end

  # Bake question
  unless only_number_solution
    problem_number = "<span class='os-number'>#{number}</span>"
    if question.solution
      problem_number = "<a class='os-number' href='##{id}-solution'>#{number}</a>"
    end
  end

  context = question.exercise_context_in_question&.cut&.paste

  question.prepend(child:
    <<~HTML
      #{problem_number unless only_number_solution}
      #{"<span class='os-divider'>. </span>" unless only_number_solution}
      <div class="os-problem-container">
        #{context if context.present?}
        #{"<span class='os-divider'>. </span>" if context.present?}
        #{question.stimulus&.cut&.paste}
        #{question.stem.cut.paste}
        #{question.answers&.cut&.paste}
      </div>
    HTML
  )

  # Bake solution
  solution = question.solution
  return unless solution

  question.add_class('os-hasSolution')
  solution.id = "#{id}-solution"
  solution.replace_children(with:
    <<~HTML
      <a class='os-number' href='##{id}'>#{number}</a>
      <span class='os-divider'>. </span>
      <div class="os-solution-container">#{solution.children}</div>
    HTML
  )
end