Sha256: 712833a41d2cb5e550fcea1c2ab7a2e967e774cb5c0198fabe50fe56cf64a301

Contents?: true

Size: 1.69 KB

Versions: 8

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module Kitchen::Directions::BakeNumberedExercise
  class V1
    def bake(exercise:, number:, suppress_solution_if: false, note_suppressed_solutions: false)
      problem = exercise.problem
      solution = exercise.solution

      exercise.pantry(name: :link_text).store(
        "#{I18n.t(:exercise_label)} #{exercise.ancestor(:chapter).count_in(:book)}.#{number}",
        label: exercise.id
      )
      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 = "<a class='os-number' href='##{exercise.id}-solution'>#{number}</a>"
          bake_solution(exercise: exercise, number: number)
        end
      end

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

    def bake_solution(exercise:, number:, divider: '. ')
      solution = exercise.solution
      solution.id = "#{exercise.id}-solution"
      exercise.add_class('os-hasSolution')

      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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
openstax_kitchen-9.2.0 lib/kitchen/directions/bake_numbered_exercise/v1.rb
openstax_kitchen-9.1.0 lib/kitchen/directions/bake_numbered_exercise/v1.rb
openstax_kitchen-9.0.0 lib/kitchen/directions/bake_numbered_exercise/v1.rb
openstax_kitchen-8.0.1 lib/kitchen/directions/bake_numbered_exercise/v1.rb
openstax_kitchen-8.0.0 lib/kitchen/directions/bake_numbered_exercise/v1.rb
openstax_kitchen-7.0.0 lib/kitchen/directions/bake_numbered_exercise/v1.rb
openstax_kitchen-6.1.0 lib/kitchen/directions/bake_numbered_exercise/v1.rb
openstax_kitchen-6.0.0 lib/kitchen/directions/bake_numbered_exercise/v1.rb