Class: Dmcli::DndDataLoader
- Inherits:
-
Object
- Object
- Dmcli::DndDataLoader
- Defined in:
- lib/dmcli/dnd_data_loader.rb
Overview
DndDataLoader Utility class for parsing and writing XML data files
Instance Method Summary collapse
- #add_spell?(spell_class, doc_spell_class, level, doc_spell_level) ⇒ Boolean
- #load(directory) ⇒ Object
- #load_spells(filepath, edition = "any", spell_class = "any", level = "any") ⇒ Object
Instance Method Details
#add_spell?(spell_class, doc_spell_class, level, doc_spell_level) ⇒ Boolean
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# 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) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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. = 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") ⇒ Object
37 38 39 40 41 42 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 |
# 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 |