Class: Serialbench::Serializers::Xml::NokogiriSerializer::StreamingHandler
- Inherits:
-
Object
- Object
- Serialbench::Serializers::Xml::NokogiriSerializer::StreamingHandler
- Defined in:
- lib/serialbench/serializers/xml/nokogiri_serializer.rb
Overview
SAX handler for streaming
Instance Attribute Summary collapse
-
#elements_processed ⇒ Object
readonly
Returns the value of attribute elements_processed.
Instance Method Summary collapse
- #cdata_block(string) ⇒ Object
- #characters(string) ⇒ Object
- #end_element(_name) ⇒ Object
-
#initialize(&block) ⇒ StreamingHandler
constructor
A new instance of StreamingHandler.
- #start_element(name, attributes = []) ⇒ Object
Constructor Details
#initialize(&block) ⇒ StreamingHandler
Returns a new instance of StreamingHandler.
106 107 108 109 110 111 112 |
# File 'lib/serialbench/serializers/xml/nokogiri_serializer.rb', line 106 def initialize(&block) require 'nokogiri' @block = block @elements_processed = 0 @current_element = nil @element_stack = [] end |
Instance Attribute Details
#elements_processed ⇒ Object (readonly)
Returns the value of attribute elements_processed.
104 105 106 |
# File 'lib/serialbench/serializers/xml/nokogiri_serializer.rb', line 104 def elements_processed @elements_processed end |
Instance Method Details
#cdata_block(string) ⇒ Object
136 137 138 |
# File 'lib/serialbench/serializers/xml/nokogiri_serializer.rb', line 136 def cdata_block(string) @element_stack.last[:text] += string if @element_stack.any? end |
#characters(string) ⇒ Object
130 131 132 133 134 |
# File 'lib/serialbench/serializers/xml/nokogiri_serializer.rb', line 130 def characters(string) return if string.strip.empty? @element_stack.last[:text] += string if @element_stack.any? end |
#end_element(_name) ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/serialbench/serializers/xml/nokogiri_serializer.rb', line 121 def end_element(_name) element = @element_stack.pop if @element_stack.empty? @block&.call(element) else @element_stack.last[:children] << element end end |
#start_element(name, attributes = []) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/serialbench/serializers/xml/nokogiri_serializer.rb', line 114 def start_element(name, attributes = []) @elements_processed += 1 attrs = Hash[attributes] @current_element = { name: name, attributes: attrs, children: [], text: '' } @element_stack.push(@current_element) end |