class CommonCartridge::Parsers::Items

Public Class Methods

new(zipfile, mod, resources) click to toggle source
# File lib/common_cartridge/parsers/items.rb, line 4
def initialize(zipfile, mod, resources)
  @zipfile = zipfile
  @mod = mod
  @resources = resources
end

Public Instance Methods

parse!() click to toggle source
# File lib/common_cartridge/parsers/items.rb, line 10
def parse!
  @mod.items.each do |item|
    map_item_to_type(item)
    unless item.items.empty?
      item.items.each { |i| map_item_to_type(i) }
    end
  end
end

Private Instance Methods

find_resource_by_ref(ref) click to toggle source
# File lib/common_cartridge/parsers/items.rb, line 43
def find_resource_by_ref(ref)
  @resources.detect do |resource|
    resource.identifier == ref
  end
end
map_item_to_type(item) click to toggle source
# File lib/common_cartridge/parsers/items.rb, line 20
def map_item_to_type(item)
  if resource = find_resource_by_ref(item.identifierref)
    if types = Elements::Resources.type_mappings.detect { |pattern, type| resource.type =~ pattern }
      item.type = types.last.type
    else !resource.type.empty?
      item.type = resource.type
    end

    if item.type == :page && resource.href =~ /web_resources/
      item.type = 'file'
    end
  end

  if item.type.nil? || item.type.empty?
    Parser.use_file(@zipfile, 'course_settings/module_meta.xml') do |xml|
      doc = Nokogiri::XML(xml)
      doc.remove_namespaces!
      item.type = snake_case(doc.xpath("//item[@identifier = '#{item.identifier}' or @identifier='#{item.identifierref}']/content_type").text)

    end
  end
end
snake_case(text) click to toggle source
# File lib/common_cartridge/parsers/items.rb, line 49
def snake_case(text)
  text.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end