Class: SAXMachine::NSStack

Inherits:
Hash
  • Object
show all
Defined in:
lib/sax-machine/ns_stack.rb

Constant Summary collapse

XMLNS =
'xmlns'

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, attrs = nil) ⇒ NSStack

Returns a new instance of NSStack.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sax-machine/ns_stack.rb', line 5

def initialize(parent=nil, attrs=nil)
  # Initialize
  super()
  @parent = parent

  return self unless attrs
  # Parse attributes
  attrs.each do |attr|
    if attr.kind_of?(Array)
      k, v = attr
      case k
      when XMLNS then self[EMPTY_STRING] = v
      when /^xmlns:(.+)/ then self[$1] = v
      end
    end
  end
end

Instance Method Details

#[](name) ⇒ Object

Lookup



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sax-machine/ns_stack.rb', line 24

def [](name)
  if (ns = super(name.to_s))
    # I've got it
    ns
  elsif @parent
    # Parent may have it
    @parent[name]
  else
    # Undefined, empty namespace
    EMPTY_STRING
  end
end

#popObject



37
38
39
# File 'lib/sax-machine/ns_stack.rb', line 37

def pop
  @parent
end