class Dmcli::DndDataLoader
DndDataLoader
Utility class for parsing and writing XML data files
Public Instance Methods
add_spell?( spell_class, doc_spell_class, level, doc_spell_level )
click to toggle source
# File lib/dmcli/dnd_data_loader.rb, line 72 def add_spell?( spell_class, doc_spell_class, level, doc_spell_level ) case spell_class when "any" return true else if spell_class == doc_spell_class case level when "any" return true else if level.to_i == doc_spell_level.to_i return true end end end end false end
load(directory)
click to toggle source
# File lib/dmcli/dnd_data_loader.rb, line 12 def load(directory) # directory = "../../spells/*.xml" puts "Loading spells..." @docs = [] files = 0 starting = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) Dir["#{directory}/*"].each do |file| if File.extname(file).strip.downcase[1..-1] == "xml" doc = Nokogiri::XML(File.open(file)) do |config| config.options = Nokogiri::XML::ParseOptions::STRICT | Nokogiri::XML::ParseOptions::NOBLANKS end @docs.push(doc) files += 1 end end ending = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) elapsed = ending - starting puts "Found #{@docs.size} spells" puts "Processed #{files} files in #{format("%0.0f", elapsed)} ms" puts end
load_spells( filepath, edition = "any", spell_class = "any", level = "any" )
click to toggle source
# File lib/dmcli/dnd_data_loader.rb, line 37 def load_spells( filepath, edition = "any", spell_class = "any", level = "any" ) puts "Loading data files from #{filepath}" load(filepath) @spell_list = SpellList.new @spells = [] @docs.each do |doc| name = doc.xpath("//a:spell/a:name", { "a" => "dungeon" }).text doc_spell_class = doc.xpath("//a:spell/a:editions/a:edition#{edition}/a:class", { "a" => "dungeon" }).text.strip doc_spell_level = doc.xpath("//a:spell/a:editions/a:edition#{edition}/a:level", { "a" => "dungeon" }).text.strip range = doc.xpath("//a:spell/a:editions/a:edition#{edition}/a:range", { "a" => "dungeon" }).text.strip duration = doc.xpath("//a:spell/a:editions/a:edition#{edition}/a:duration", { "a" => "dungeon" }).text.strip effect = doc.xpath("//a:spell/a:editions/a:edition#{edition}/a:effect", { "a" => "dungeon" }).text.strip reversible = doc.xpath("//a:spell/a:editions/a:edition#{edition}/a:isReversible", { "a" => "dungeon" }).text.strip description_text = doc.xpath("//a:spell/a:editions/a:edition#{edition}/a:description", { "a" => "dungeon" }).text.strip.split("\n") name += " *" if reversible == "true" if add_spell?(spell_class, doc_spell_class, level, doc_spell_level) @spell_list.add Spell.new(name, reversible, doc_spell_class, doc_spell_level, range, duration, effect, description_text) end end @spell_list.sort @spell_list.spells end