Class: Serialbench::Serializers::Xml::OxSerializer::StreamingHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/serialbench/serializers/xml/ox_serializer.rb

Overview

SAX handler for streaming

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ StreamingHandler



51
52
53
54
55
56
# File 'lib/serialbench/serializers/xml/ox_serializer.rb', line 51

def initialize(&block)
  @block = block
  @elements_processed = 0
  @current_element = nil
  @element_stack = []
end

Instance Attribute Details

#elements_processedObject (readonly)

Returns the value of attribute elements_processed.



49
50
51
# File 'lib/serialbench/serializers/xml/ox_serializer.rb', line 49

def elements_processed
  @elements_processed
end

Instance Method Details

#attr(name, value) ⇒ Object



79
80
81
# File 'lib/serialbench/serializers/xml/ox_serializer.rb', line 79

def attr(name, value)
  @element_stack.last[:attributes][name] = value if @element_stack.any?
end

#end_element(_name) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/serialbench/serializers/xml/ox_serializer.rb', line 64

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) ⇒ Object



58
59
60
61
62
# File 'lib/serialbench/serializers/xml/ox_serializer.rb', line 58

def start_element(name)
  @elements_processed += 1
  @current_element = { name: name, attributes: {}, children: [], text: '' }
  @element_stack.push(@current_element)
end

#text(value) ⇒ Object



73
74
75
76
77
# File 'lib/serialbench/serializers/xml/ox_serializer.rb', line 73

def text(value)
  return if value.strip.empty?

  @element_stack.last[:text] += value if @element_stack.any?
end