# frozen_string_literal: true require 'hs/loaders/xml_parser' module HS module CourseLoader include HS::XMLParser KNOWN_LOADERS = %i[title chapters].freeze def load_course(xml, &block) create_course(parse_xml(xml), &block) end private def create_course(data, &block) course = HS::Course.new(data.slice(:slug)) data[:_children].each { |child| load_a_part(course, child, &block) } course end def load_a_part(course, child, &block) return unless KNOWN_LOADERS.include?(child[:_name]) send("load_#{child[:_name]}", course, child, &block) end def load_title(course, child) course.title = get_text(child) end def load_chapters(course, child) return unless block_given? files = child[:_children].map { |c| c[:file] }.compact files.each { |file| yield course, file } end end end