Class: Kitchen::Directions::BakeNumberedExercise::V1

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/directions/bake_numbered_exercise/v1.rb

Instance Method Summary collapse

Instance Method Details

#bake(exercise:, number:, suppress_solution_if:, note_suppressed_solutions:, cases:, solution_stays_put:) ⇒ Object

rubocop:disable Metrics/ParameterLists



6
7
8
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
# File 'lib/kitchen/directions/bake_numbered_exercise/v1.rb', line 6

def bake(exercise:, number:, suppress_solution_if:,
         note_suppressed_solutions:, cases:, solution_stays_put:)
  problem = exercise.problem
  solution = exercise.solution

  in_appendix = exercise.has_ancestor?(:page) && exercise.ancestor(:page).has_class?('appendix')

  # Store label information
  if in_appendix
    label_number = number
    title_label = "<span class=\"os-title-label\">#{I18n.t('exercise')}</span>"
    problem_divider = ''
  else
    label_number = "#{exercise.ancestor(:chapter).count_in(:book)}.#{number}"
    title_label = ''
    problem_divider = "<span class='os-divider'>. </span>"
  end

  exercise.target_label(label_text: 'exercise', custom_content: label_number, cases: cases)

  problem_number = "<span class='os-number'>#{number}</span>"

  suppress_solution =
    case suppress_solution_if
    when Symbol
      number.send(suppress_solution_if)
    else
      suppress_solution_if
    end

  if solution.present?
    if suppress_solution
      solution.trash
      exercise.add_class('os-hasSolution-trashed') if note_suppressed_solutions
    else
      problem_number = \
        if solution_stays_put || in_appendix
          "<span class='os-number'>#{number}</span>"
        else
          "<a class='os-number' href='##{exercise.id}-solution'>#{number}</a>"
        end
      bake_solution(
        exercise: exercise,
        number: number,
        solution_stays_put: solution_stays_put,
        in_appendix: in_appendix
      )
    end
  end

  problem.replace_children(with:
    <<~HTML
      #{title_label}
      #{problem_number}
      #{problem_divider}
      <div class="os-problem-container">#{problem.children}</div>
    HTML
  )
end

#bake_solution(exercise:, number:, solution_stays_put:, divider: '. ', in_appendix: false) ⇒ Object

rubocop:enable Metrics/ParameterLists



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/kitchen/directions/bake_numbered_exercise/v1.rb', line 67

def bake_solution(exercise:, number:, solution_stays_put:, divider: '. ', in_appendix: false)
  solution = exercise.solution
  if solution_stays_put
    solution.wrap_children(class: 'os-solution-container')
    solution.prepend(child:
      <<~HTML
        <h4 class="solution-title" data-type="title">
          <span class="os-text">#{I18n.t(:solution)}</span>
        </h4>
      HTML
    )
    return
  end

  solution.id = "#{exercise.id}-solution"
  exercise.add_class('os-hasSolution')

  if in_appendix
    solution.wrap_children(class: 'os-solution-container')
    return
  end

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