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.
90 91 92 93 94 95 |
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 90 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.
88 89 90 |
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 88 def elements_processed @elements_processed end |
Instance Method Details
#after_element(_namespace, _name) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 113 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
109 110 111 |
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 109 def on_cdata(text) @element_stack.last[:text] += text if @element_stack.any? end |
#on_element(namespace, name, attributes = {}) ⇒ Object
97 98 99 100 101 |
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 97 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
103 104 105 106 107 |
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 103 def on_text(text) return if text.strip.empty? @element_stack.last[:text] += text if @element_stack.any? end |