Class: Serialbench::Serializers::Xml::OgaSerializer::StreamingHandler
- Inherits:
-
Object
- Object
- Serialbench::Serializers::Xml::OgaSerializer::StreamingHandler
- Defined in:
- lib/serialbench/serializers/xml/oga_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
- #after_element(_namespace, _name) ⇒ Object
-
#initialize(&block) ⇒ StreamingHandler
constructor
A new instance of StreamingHandler.
- #on_cdata(text) ⇒ Object
- #on_element(namespace, name, attributes = {}) ⇒ Object
- #on_text(text) ⇒ Object
Constructor Details
#initialize(&block) ⇒ StreamingHandler
Returns a new instance of StreamingHandler.
92 93 94 95 96 97 |
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 92 def initialize(&block) @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.
90 91 92 |
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 90 def elements_processed @elements_processed end |
Instance Method Details
#after_element(_namespace, _name) ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 115 def after_element(_namespace, _name) element = @element_stack.pop if @element_stack.empty? @block&.call(element) else @element_stack.last[:children] << element end end |
#on_cdata(text) ⇒ Object
111 112 113 |
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 111 def on_cdata(text) @element_stack.last[:text] += text if @element_stack.any? end |
#on_element(namespace, name, attributes = {}) ⇒ Object
99 100 101 102 103 |
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 99 def on_element(namespace, name, attributes = {}) @elements_processed += 1 @current_element = { name: name, namespace: namespace, attributes: attributes, children: [], text: '' } @element_stack.push(@current_element) end |
#on_text(text) ⇒ Object
105 106 107 108 109 |
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 105 def on_text(text) return if text.strip.empty? @element_stack.last[:text] += text if @element_stack.any? end |