Class: D3MPQ::Analyzer
- Inherits:
-
Object
- Object
- D3MPQ::Analyzer
- Defined in:
- lib/d3_mpq/analyzer.rb
Constant Summary collapse
- SEPERATOR =
","
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Return analyzed attributes.
Instance Method Summary collapse
-
#initialize(parser, files, fields = :content) ⇒ Analyzer
constructor
A new instance of Analyzer.
-
#parser_name ⇒ Object
Example: d3mpq_stringlist.
-
#write ⇒ Object
Just write analyzed data to disc.
-
#write_analyzed(dir) ⇒ Object
Writing multiple files to given dir.
-
#write_game_balance ⇒ Object
Writing if GameBalance.
-
#write_recipe ⇒ Object
Writing if StringList.
-
#write_single_file(dir, filename = nil) ⇒ Object
Write output to a single file.
-
#write_stringlist ⇒ Object
Writing if StringList.
Constructor Details
#initialize(parser, files, fields = :content) ⇒ Analyzer
Returns a new instance of Analyzer.
9 10 11 12 13 14 |
# File 'lib/d3_mpq/analyzer.rb', line 9 def initialize(parser, files, fields = :content) @parser = parser.new @files = [*files] @field = fields # @fields = [*fields] end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Return analyzed attributes
109 110 111 |
# File 'lib/d3_mpq/analyzer.rb', line 109 def attributes @attributes end |
Instance Method Details
#parser_name ⇒ Object
Example: d3mpq_stringlist
58 59 60 |
# File 'lib/d3_mpq/analyzer.rb', line 58 def parser_name @parser_name ||= @parser.class.name.gsub("::", "_").downcase end |
#write ⇒ Object
Just write analyzed data to disc. (within /analyze)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/d3_mpq/analyzer.rb', line 17 def write case parser_name when "d3mpq_stringlist" write_stringlist when "d3mpq_recipe" write_recipe when "d3mpq_coredata_gamebalance_setitembonuses" @field = nil write_recipe when "d3mpq_attributes" write_single_file("analyze") when "d3mpq_coredata_actor" write_single_file("analyze") else write_game_balance end end |
#write_analyzed(dir) ⇒ Object
Writing multiple files to given dir.
98 99 100 101 102 103 104 105 106 |
# File 'lib/d3_mpq/analyzer.rb', line 98 def write_analyzed(dir) FileUtils.mkdir_p(dir) attributes.each do |a, v| path = File.join(dir, a.to_s) s = "Count|Value\n" + v.map { |e| "#{e[:count]}|#{e[:value]}" }.join("\n") File.open("#{path}.csv", 'w') { |f| f.write(s) } end end |
#write_game_balance ⇒ Object
Writing if GameBalance
49 50 51 52 53 54 55 |
# File 'lib/d3_mpq/analyzer.rb', line 49 def write_game_balance write_single_file("analyze") dir = File.join("analyze", parser_name) dir = File.join(dir, @field.to_s) if @field write_analyzed(dir) end |
#write_recipe ⇒ Object
Writing if StringList
42 43 44 45 46 |
# File 'lib/d3_mpq/analyzer.rb', line 42 def write_recipe @field = nil dir = File.join("analyze", "Recipe") write_single_file(dir, @files.first.split("/").last) end |
#write_single_file(dir, filename = nil) ⇒ Object
Write output to a single file. (dir/filename) Using parser as filename if none is given.
64 65 66 67 68 69 70 71 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/d3_mpq/analyzer.rb', line 64 def write_single_file(dir, filename = nil) FileUtils.mkdir_p File.join(dir) # HACKY: NEED TO FIX keys = snapshots.first.keys keys = snapshots.first[@field].first.keys if @field s = [] s << keys.join(SEPERATOR) snapshots.each do |snapshot| # HACKY: NEED TO FIX if @field snapshot[@field].each_with_index do |e, i| s << e.values.map do |v| if v.is_a?(String) "\"#{v}\"" else "#{v}" end end.join(SEPERATOR) end else s << [*snapshot.values].map { |e| e.is_a?(String) ? "\"#{e}\"" : "#{e}" } .join(SEPERATOR) end end filename ||= @parser.class.name.split("::").last path = File.join(dir, filename) File.open("#{path}.csv", 'w') { |f| f.write(s.join("\n")) } end |
#write_stringlist ⇒ Object
Writing if StringList
36 37 38 39 |
# File 'lib/d3_mpq/analyzer.rb', line 36 def write_stringlist dir = File.join("analyze", "StringList") write_single_file(dir, @files.first.split("/").last) end |