Module: ProformaXML::Helpers::ImportHelpers

Included in:
Importer
Defined in:
lib/proformaxml/helpers/import_helpers.rb

Constant Summary collapse

CONFIGURATION_NODES =
%w[filerefs timeout externalresourcerefs test-meta-data].freeze

Instance Method Summary collapse

Instance Method Details

#add_test_configuration(test, test_node) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/proformaxml/helpers/import_helpers.rb', line 58

def add_test_configuration(test, test_node)
  test_configuration_node = test_node.xpath("#{@pro_ns}:test-configuration")
  test.files = test_files_from_test_configuration(test_configuration_node)
  test.configuration = extra_configuration_from_test_configuration(test_configuration_node)
   = test_node.xpath("#{@pro_ns}:test-configuration").xpath("#{@pro_ns}:test-meta-data")
  test. = convert_xml_node_to_json() if .present?
end

#attached_file_attributes(attributes, file_tag) ⇒ Object



39
40
41
42
43
# File 'lib/proformaxml/helpers/import_helpers.rb', line 39

def attached_file_attributes(attributes, file_tag)
  filename = file_tag.text
  shared_file_attributes(attributes, file_tag).merge(filename:,
    content: filestring_from_zip(filename))
end

#embedded_file_attributes(attributes, file_tag) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/proformaxml/helpers/import_helpers.rb', line 30

def embedded_file_attributes(attributes, file_tag)
  shared = shared_file_attributes(attributes, file_tag)
  shared.merge(
    content: content_from_file_tag(file_tag, shared[:binary])
  ).tap do |hash|
    hash[:filename] = file_tag.attributes['filename']&.value if file_tag.attributes['filename']&.value.present?
  end
end

#extra_configuration_from_test_configuration(test_configuration_node) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/proformaxml/helpers/import_helpers.rb', line 66

def extra_configuration_from_test_configuration(test_configuration_node)
  configuration_any_nodes = test_configuration_node.children.reject {|c| CONFIGURATION_NODES.include? c.name }
  return nil if configuration_any_nodes.empty?

  {}.tap do |config_hash|
    configuration_any_nodes.each do |config_node|
      config_hash.merge!(convert_xml_node_to_json(config_node)) {|key, oldval, newval| key == '@@order' ? oldval + newval : newval }
    end
  end
end

#set_hash_value_if_present(hash:, name:, attributes: nil, value_overwrite: nil) ⇒ Object



8
9
10
11
12
13
# File 'lib/proformaxml/helpers/import_helpers.rb', line 8

def set_hash_value_if_present(hash:, name:, attributes: nil, value_overwrite: nil)
  raise unless attributes || value_overwrite

  value = value_overwrite || attributes[name.to_s]&.value
  hash[name.underscore.to_sym] = value if value.present?
end

#set_value(object:, name:, value:) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/proformaxml/helpers/import_helpers.rb', line 22

def set_value(object:, name:, value:)
  if object.is_a? Hash
    object[name] = value
  else
    object.send(:"#{name}=", value)
  end
end

#set_value_from_xml(object:, node:, name:, attribute: false, check_presence: true) ⇒ Object



15
16
17
18
19
20
# File 'lib/proformaxml/helpers/import_helpers.rb', line 15

def set_value_from_xml(object:, node:, name:, attribute: false, check_presence: true)
  value = value_from_node(name, node, attribute)
  return if check_presence && value.blank?

  set_value(object:, name: (name.is_a?(Array) ? name[1] : name).underscore, value:)
end

#shared_file_attributes(attributes, file_tag) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/proformaxml/helpers/import_helpers.rb', line 45

def shared_file_attributes(attributes, file_tag)
  {
    id: attributes['id']&.value,
    used_by_grader: attributes['used-by-grader']&.value == 'true',
    visible: attributes['visible']&.value,
    binary: file_tag.name.include?('-bin-file'),
  }.tap do |hash|
    set_hash_value_if_present(hash:, name: 'usage-by-lms', attributes:)
    set_value_from_xml(object: hash, node: file_tag.parent, name: 'internal-description')
    set_hash_value_if_present(hash:, name: 'mimetype', attributes:)
  end
end

#test_files_from_test_configuration(test_configuration_node) ⇒ Object



77
78
79
# File 'lib/proformaxml/helpers/import_helpers.rb', line 77

def test_files_from_test_configuration(test_configuration_node)
  files_from_filerefs(test_configuration_node.xpath("#{@pro_ns}:filerefs"))
end