Class: FideXmlParser::Processor
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- FideXmlParser::Processor
- Defined in:
- lib/fide_xml_parser/processor.rb
Direct Known Subclasses
Constant Summary collapse
- ANSI_GO_TO_LINE_START =
"\033[1G"
Instance Attribute Summary collapse
-
#array_name ⇒ Object
Constructor parameters:.
-
#current_property_name ⇒ Object
For internal use:.
-
#field_name_renames ⇒ Object
User-provided callbacks:.
-
#input_record_count ⇒ Object
For internal use:.
-
#key_filter ⇒ Object
User-provided callbacks:.
-
#numeric_fields ⇒ Object
Constructor parameters:.
-
#output_record_count ⇒ Object
For internal use:.
-
#record ⇒ Object
For internal use:.
-
#record_filter ⇒ Object
User-provided callbacks:.
-
#record_name ⇒ Object
Constructor parameters:.
-
#records ⇒ Object
For internal use:.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Instance Method Summary collapse
- #characters(string) ⇒ Object
- #current_time ⇒ Object
- #end_element(name) ⇒ Object
- #finish ⇒ Object
-
#initialize(array_name, record_name, numeric_fields) ⇒ Processor
constructor
A new instance of Processor.
- #output_status ⇒ Object
- #parse(data_source) ⇒ Object
- #start_element(name, _attrs) ⇒ Object
Constructor Details
#initialize(array_name, record_name, numeric_fields) ⇒ Processor
Returns a new instance of Processor.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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 |
Instance Attribute Details
#array_name ⇒ Object
Constructor parameters:
35 36 37 |
# File 'lib/fide_xml_parser/processor.rb', line 35 def array_name @array_name end |
#current_property_name ⇒ Object
For internal use:
41 42 43 |
# File 'lib/fide_xml_parser/processor.rb', line 41 def current_property_name @current_property_name end |
#field_name_renames ⇒ Object
User-provided callbacks:
38 39 40 |
# File 'lib/fide_xml_parser/processor.rb', line 38 def field_name_renames @field_name_renames end |
#input_record_count ⇒ Object
For internal use:
41 42 43 |
# File 'lib/fide_xml_parser/processor.rb', line 41 def input_record_count @input_record_count end |
#key_filter ⇒ Object
User-provided callbacks:
38 39 40 |
# File 'lib/fide_xml_parser/processor.rb', line 38 def key_filter @key_filter end |
#numeric_fields ⇒ Object
Constructor parameters:
35 36 37 |
# File 'lib/fide_xml_parser/processor.rb', line 35 def numeric_fields @numeric_fields end |
#output_record_count ⇒ Object
For internal use:
41 42 43 |
# File 'lib/fide_xml_parser/processor.rb', line 41 def output_record_count @output_record_count end |
#record ⇒ Object
For internal use:
41 42 43 |
# File 'lib/fide_xml_parser/processor.rb', line 41 def record @record end |
#record_filter ⇒ Object
User-provided callbacks:
38 39 40 |
# File 'lib/fide_xml_parser/processor.rb', line 38 def record_filter @record_filter end |
#record_name ⇒ Object
Constructor parameters:
35 36 37 |
# File 'lib/fide_xml_parser/processor.rb', line 35 def record_name @record_name end |
#records ⇒ Object
For internal use:
41 42 43 |
# File 'lib/fide_xml_parser/processor.rb', line 41 def records @records end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
32 33 34 |
# File 'lib/fide_xml_parser/processor.rb', line 32 def start_time @start_time end |
Instance Method Details
#characters(string) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# 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 ⇒ Object
69 70 71 |
# File 'lib/fide_xml_parser/processor.rb', line 69 def current_time Process.clock_gettime(Process::CLOCK_MONOTONIC) end |
#end_element(name) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# 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 ⇒ Object
130 131 132 133 |
# File 'lib/fide_xml_parser/processor.rb', line 130 def finish output_status puts end |
#output_status ⇒ Object
74 75 76 77 78 79 80 81 |
# 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) ⇒ Object
62 63 64 65 66 |
# 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) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# 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 |