Class: Gamesdb::SaxDocument

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/gamesdb/saxdocument.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSaxDocument

Returns a new instance of SaxDocument.



4
5
6
# File 'lib/gamesdb/saxdocument.rb', line 4

def initialize
  @stack = []
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



2
3
4
# File 'lib/gamesdb/saxdocument.rb', line 2

def stack
  @stack
end

Instance Method Details

#bodyObject



13
14
15
# File 'lib/gamesdb/saxdocument.rb', line 13

def body
  @stack.first
end

#characters(string) ⇒ Object



8
9
10
11
# File 'lib/gamesdb/saxdocument.rb', line 8

def characters(string)
  @value ||= ''
  @value << string.strip
end

#end_element(name) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gamesdb/saxdocument.rb', line 17

def end_element(name)
  last = @stack.pop
  if last.empty? && @value.empty?
    @stack.last[name] = ''
  elsif last == {:i_nil=>"true"}
    @stack.last[name] = nil
  elsif !@value.empty?
    @stack.last[name] = @value
  end
  @value = ''
end

#start_element(name, attributes = []) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gamesdb/saxdocument.rb', line 29

def start_element(name, attributes = [])
  @value = ''
  parsed_attributes = {}
  until attributes.empty?
    if attributes.first.is_a?(Array)
      key, value = attributes.shift
    else
      key, value = attributes.shift, attributes.shift
    end
    parsed_attributes[key.gsub(':','_')] = value
  end
  if @stack.last.is_a?(Array)
    @stack.last << {name => parsed_attributes}
  else
    data = if @stack.empty?
             @stack.push(parsed_attributes)
             parsed_attributes
           elsif @stack.last[name]
             unless @stack.last[name].is_a?(Array)
               @stack.last[name] = [@stack.last[name]]
             end
             @stack.last[name] << parsed_attributes
             @stack.last[name].last
           else
             @stack.last[name] = {}
             @stack.last[name].merge!(parsed_attributes)
             @stack.last[name]
           end
    @stack.push(data)
  end
end