Class: AtomicAssessmentsImport::CSV::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/atomic_assessments_import/csv/converter.rb

Constant Summary collapse

HEADERS =
[
  "question id",
  "title",
  /tag: *[a-zA-Z0-9_].*/,
  "question text",
  "question type",
  "template",
  "correct answer",
  /option [a-o]/,
  /option [a-o] feedback/,
  "scoring type",
  "shuffle options",
  "points",
  "general feedback",
  "correct feedback",
  "partially correct feedback",
  "incorrect feedback",
  "distractor rationale",
  "stimulus review",
  "acknowledgements",
  "instructor stimulus",
  "sample answer",
  "description",
  "alignment url",
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Converter

Returns a new instance of Converter.



39
40
41
# File 'lib/atomic_assessments_import/csv/converter.rb', line 39

def initialize(file)
  @file = file
end

Instance Method Details

#convertObject



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
74
75
76
77
78
79
80
81
# File 'lib/atomic_assessments_import/csv/converter.rb', line 43

def convert
  items = []
  questions = []

  ::CSV.foreach(
    @file,
    headers: true,
    header_converters: lambda do |header|
      normalized = header.strip
      normalized =
        if (m = normalized.match(/^tag:(.+)$/i))
          "tag:#{m[1].strip}"
        else
          normalized.downcase
        end
      if !HEADERS.find { |h| h.is_a?(Regexp) ? h =~ normalized : h == normalized }
        raise ArgumentError, "Unknown column: #{header}"
      end

      normalized
    end,
    converters: ->(data) { data&.strip }
  ) do |row|
    item, question_widgets = convert_row(row)

    items << item
    questions += question_widgets
  rescue StandardError => e
    raise e, "Error processing row: #{row.inspect} - #{e.message}"
  end

  {
    activities: [],
    items:,
    questions:,
    features: [],
    errors: [],
  }
end