class FideXmlParser::Processor
A field_name_renames
hash can be provided. Keys are the field names in the XML input, values are the names in the output JSON, e.g.:
{ 'rating' => 'standard_rating', 'games' => 'standard_games' }
Constants
- ANSI_GO_TO_LINE_START
Attributes
array_name[RW]
Constructor parameters:
current_property_name[RW]
For internal use:
field_name_renames[RW]
User-provided callbacks:
input_record_count[RW]
For internal use:
key_filter[RW]
User-provided callbacks:
numeric_fields[RW]
Constructor parameters:
output_record_count[RW]
For internal use:
record[RW]
For internal use:
record_filter[RW]
User-provided callbacks:
record_name[RW]
Constructor parameters:
records[RW]
For internal use:
start_time[R]
Public Class Methods
new(array_name, record_name, numeric_fields)
click to toggle source
# File lib/fide_xml_parser/processor.rb, line 45 def initialize(array_name, record_name, numeric_fields) @array_name = array_name @record_name = record_name @numeric_fields = numeric_fields @key_filter = nil @record_filter = nil @field_name_renames = nil @current_property_name = nil @record = {} @records = [] @start_time = current_time @keys_to_exclude = [] @input_record_count = 0 @output_record_count = 0 end
Public Instance Methods
characters(string)
click to toggle source
# File lib/fide_xml_parser/processor.rb, line 113 def characters(string) if current_property_name if key_filter.nil? || key_filter.(current_property_name) value = numeric_fields.include?(current_property_name) ? Integer(string) : string key = current_property_name if field_name_renames new_field_name = field_name_renames[key] if new_field_name key = new_field_name end end record[key] = value end end end
current_time()
click to toggle source
# File lib/fide_xml_parser/processor.rb, line 69 def current_time Process.clock_gettime(Process::CLOCK_MONOTONIC) end
end_element(name)
click to toggle source
# File lib/fide_xml_parser/processor.rb, line 97 def end_element(name) case name when array_name # end of data, write JSON file finish when record_name if record_filter.nil? || record_filter.(record) self.output_record_count += 1 records << record end self.record = {} else self.current_property_name = nil end end
finish()
click to toggle source
# File lib/fide_xml_parser/processor.rb, line 130 def finish output_status puts end
output_status()
click to toggle source
# File lib/fide_xml_parser/processor.rb, line 74 def output_status print ANSI_GO_TO_LINE_START print "Records processed: %9d kept: %9d Seconds elapsed: %11.2f" % [ input_record_count, output_record_count, current_time - start_time ] end
parse(data_source)
click to toggle source
# File lib/fide_xml_parser/processor.rb, line 62 def parse(data_source) parser = Nokogiri::XML::SAX::Parser.new(self) parser.parse(data_source) records end
start_element(name, _attrs)
click to toggle source
# File lib/fide_xml_parser/processor.rb, line 84 def start_element(name, _attrs) case name when array_name # ignore when record_name self.input_record_count += 1 output_status if input_record_count % 1000 == 0 else # this is a field in the players record; process it as such self.current_property_name = name end end