Class: D3MPQ::CSVGenerator::Base
- Inherits:
-
Object
- Object
- D3MPQ::CSVGenerator::Base
- Defined in:
- lib/d3_mpq/csv_generator/base.rb
Direct Known Subclasses
Actor, AffixList, ExperienceTable, ItemEnhancements, ItemTypes, Items, MonsterAffixes, MonsterLevels, MonsterNames, PowerFormulaTables, RareNames, RecipeDetails, Recipes, SetItemBonuses, SocketedEffects, StringList
Constant Summary collapse
- SEPERATOR =
";"
- LINE_SEPERATOR =
"\n"
Instance Attribute Summary collapse
-
#files ⇒ Object
Returns the value of attribute files.
Class Method Summary collapse
-
.map_to_csv(options = nil) ⇒ Object
TODO: refactor.
Instance Method Summary collapse
-
#add_file(f) ⇒ Object
Add files.
- #base_to_subjects(base, filename) ⇒ Object
- #csv_keys ⇒ Object
-
#file_snapshots ⇒ Object
Return snapshots of parsed files.
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
-
#is_back_dir(dir) ⇒ Object
HACK.
- #map_to_csv ⇒ Object
- #subject_to_csv_line(subject, filename, base = nil, index = nil) ⇒ Object
-
#write ⇒ Object
Write.
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/d3_mpq/csv_generator/base.rb', line 8 def initialize( = {}) raise "Abstract" if self.class.name == "D3MPQ::CSVGenerator::Base" if .is_a?(Hash) add_file([:files]) @output_path = [:output_path] else add_file() end k = self.class.name.split("::").last.downcase @output_path ||= "analyze/#{k}.csv" end |
Instance Attribute Details
#files ⇒ Object
Returns the value of attribute files.
3 4 5 |
# File 'lib/d3_mpq/csv_generator/base.rb', line 3 def files @files end |
Class Method Details
.map_to_csv(options = nil) ⇒ Object
TODO: refactor
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/d3_mpq/csv_generator/base.rb', line 25 def map_to_csv( = nil) return @map_to_csv unless if .is_a?(Array) @map_to_csv = {} .each do |e| if e.is_a?(String) || e.is_a?(Symbol) @map_to_csv[e.intern] = e.intern else @map_to_csv[e.to_a[0].intern] = e.to_a[1] end end elsif .is_a?(Hash) @map_to_csv = else raise "Options must be an Array or Hash." end end |
Instance Method Details
#add_file(f) ⇒ Object
Add files
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/d3_mpq/csv_generator/base.rb', line 55 def add_file(f) @files ||= [] if f.respond_to?(:entries) f.each { |e| add_file(e) } else f = f.to_s if File.directory?(f) && !is_back_dir(f) entries = Dir.entries(f).delete_if { |e| is_back_dir(e) } entries.each { |e| add_file( File.join(f, e) ) } else unless f.empty? || is_back_dir(f) temp_file = File.new(f) size = temp_file.size temp_file.close @files << f unless size == 0 end end end return @files end |
#base_to_subjects(base, filename) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/d3_mpq/csv_generator/base.rb', line 92 def base_to_subjects(base, filename) base.content.each_with_index do |subject, index| print "." @lines << subject_to_csv_line(subject, filename, base, index) end end |
#csv_keys ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/d3_mpq/csv_generator/base.rb', line 99 def csv_keys if map_to_csv map_to_csv.keys.join(SEPERATOR) else raise "Not Implemented" end end |
#file_snapshots ⇒ Object
Return snapshots of parsed files
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/d3_mpq/csv_generator/base.rb', line 144 def file_snapshots @file_snapshots = Hash.new { |h, k| h[k] = [] } files.each do |f| io = File.open(f) begin @mpq_reader = self.class::MPQ_READER.new @mpq_reader.read(io) @file_snapshots[f] << @mpq_reader print "." rescue EOFError => e puts "#{e.inspect}\n#{f}" end end return @file_snapshots end |
#is_back_dir(dir) ⇒ Object
HACK
164 165 166 |
# File 'lib/d3_mpq/csv_generator/base.rb', line 164 def is_back_dir(dir) dir.end_with?(".") || dir.end_with?("..") end |
#map_to_csv ⇒ Object
45 46 47 |
# File 'lib/d3_mpq/csv_generator/base.rb', line 45 def map_to_csv self.class.map_to_csv end |
#subject_to_csv_line(subject, filename, base = nil, index = nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/d3_mpq/csv_generator/base.rb', line 113 def subject_to_csv_line(subject, filename, base = nil, index = nil) line = [] if map_to_csv map_to_csv.each do |col, opt| if opt.is_a?(Proc) entry = opt.call(subject, filename, base, index) else entry = subject.send(opt) end entry = entry.to_s if entry.is_a?(BinData::String) if entry.is_a?(String) entry.gsub!('"', '\"') entry = "\"#{entry}\"" end entry = entry.to_s.encode!('UTF-8', 'UTF-8', :invalid => :replace) # HACK entry.chop! if entry.end_with?("\x00") line << entry end else raise "TODO" end return line.join(SEPERATOR) end |
#write ⇒ Object
Write
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/d3_mpq/csv_generator/base.rb', line 81 def write @lines = [] @lines << csv_keys file_snapshots.each do |filename, snapshots| snapshots.each { |base| base_to_subjects(base, filename) } end File.open(@output_path, 'w') { |f| f.write(@lines.join(LINE_SEPERATOR)) } end |