Class: MultiXml::Parsers::Ox::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_xml/parsers/ox.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHandler

Returns a new instance of Handler.



39
40
41
# File 'lib/multi_xml/parsers/ox.rb', line 39

def initialize
  @stack = []
end

Instance Attribute Details

#stackObject

Returns the value of attribute stack.



37
38
39
# File 'lib/multi_xml/parsers/ox.rb', line 37

def stack
  @stack
end

Instance Method Details

#append(key, value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/multi_xml/parsers/ox.rb', line 74

def append(key, value)
  key = key.to_s
  h = @stack.last
  if h.key?(key)
    v = h[key]
    if v.is_a?(Array)
      v << value
    else
      h[key] = [v, value]
    end
  else
    h[key] = value
  end
end

#attr(name, value) ⇒ Object



47
48
49
# File 'lib/multi_xml/parsers/ox.rb', line 47

def attr(name, value)
  append(name, value) unless @stack.empty?
end

#cdata(value) ⇒ Object



55
56
57
# File 'lib/multi_xml/parsers/ox.rb', line 55

def cdata(value)
  append(MultiXml::CONTENT_ROOT, value)
end

#docObject



43
44
45
# File 'lib/multi_xml/parsers/ox.rb', line 43

def doc
  @stack[0]
end

#end_element(_) ⇒ Object



66
67
68
# File 'lib/multi_xml/parsers/ox.rb', line 66

def end_element(_)
  @stack.pop
end

#error(message, line, column) ⇒ Object

Raises:

  • (StandardError)


70
71
72
# File 'lib/multi_xml/parsers/ox.rb', line 70

def error(message, line, column)
  raise(StandardError, "#{message} at #{line}:#{column}")
end

#start_element(name) ⇒ Object



59
60
61
62
63
64
# File 'lib/multi_xml/parsers/ox.rb', line 59

def start_element(name)
  @stack.push({}) if @stack.empty?
  h = {}
  append(name, h)
  @stack.push(h)
end

#text(value) ⇒ Object



51
52
53
# File 'lib/multi_xml/parsers/ox.rb', line 51

def text(value)
  append(MultiXml::CONTENT_ROOT, value)
end