Class: Ax

Inherits:
Ox::Sax
  • Object
show all
Defined in:
lib/magnum-pi/gem_ext/ox/ax.rb

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Ax

Returns a new instance of Ax.



5
6
7
# File 'lib/magnum-pi/gem_ext/ox/ax.rb', line 5

def initialize(xml)
  @stringio = StringIO.new xml
end

Instance Method Details

#attr(name, str) ⇒ Object



33
34
35
36
37
# File 'lib/magnum-pi/gem_ext/ox/ax.rb', line 33

def attr(name, str)
  if element = current_element
    element[name.to_s] = str
  end
end

#cdata(str) ⇒ Object



45
46
47
48
49
# File 'lib/magnum-pi/gem_ext/ox/ax.rb', line 45

def cdata(str)
  if element = current_element
    element[:text] = str
  end
end

#each(pattern, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/magnum-pi/gem_ext/ox/ax.rb', line 9

def each(pattern, &block)
  @pattern = pattern.split("/").collect{|x| x == "" ? "*" : x}
  @block = block

  @stringio.rewind
  Ox.sax_parse self, @stringio

ensure
  @current_path = nil
  @elements = nil
  @regexp = nil
end

#end_element(name) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/magnum-pi/gem_ext/ox/ax.rb', line 51

def end_element(name)
  element = finalize_element
  if entry?
    @block.call element, current_path[-1]
  end
  current_path.pop
end

#start_element(name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/magnum-pi/gem_ext/ox/ax.rb', line 22

def start_element(name)
  current_path << name
  if current_element
    add_element name
  elsif entry?
    add_element
  else
    return
  end
end

#text(str) ⇒ Object



39
40
41
42
43
# File 'lib/magnum-pi/gem_ext/ox/ax.rb', line 39

def text(str)
  if element = current_element
    element[:text] = str
  end
end

#to_hashObject Also known as: to_enum



59
60
61
62
63
64
65
# File 'lib/magnum-pi/gem_ext/ox/ax.rb', line 59

def to_hash
  hash = {}
  each "*" do |entry|
    hash.merge! entry
  end
  hash
end