Class: XmlHasher::Handler

Inherits:
Ox::Sax
  • Object
show all
Defined in:
lib/xmlhasher/handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Handler

Returns a new instance of Handler.



6
7
8
9
# File 'lib/xmlhasher/handler.rb', line 6

def initialize(options = {})
  @options = options
  @stack = []
end

Instance Method Details

#attr(name, value) ⇒ Object



19
20
21
22
23
# File 'lib/xmlhasher/handler.rb', line 19

def attr(name, value)
  unless ignore_attribute?(name)
    @stack.last.attributes[transform(name)] = escape(value) unless @stack.empty?
  end
end

#end_element(name) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/xmlhasher/handler.rb', line 29

def end_element(name)
  if @stack.size == 1
    @hash = @stack.pop.to_hash
  else
    node = @stack.pop
    @stack.last.children << node
  end
end

#start_element(name) ⇒ Object



15
16
17
# File 'lib/xmlhasher/handler.rb', line 15

def start_element(name)
  @stack.push(Node.new(transform(name)))
end

#text(value) ⇒ Object



25
26
27
# File 'lib/xmlhasher/handler.rb', line 25

def text(value)
  @stack.last.text = escape(value)
end

#to_hashObject



11
12
13
# File 'lib/xmlhasher/handler.rb', line 11

def to_hash
  @hash || {}
end