Sha256: 41432bc2638cb3f1957cd6aef9f6632bd546966d4bb5b2aeaf00e88d6d1cb765

Contents?: true

Size: 755 Bytes

Versions: 3

Compression:

Stored size: 755 Bytes

Contents

module SAXMachine
  class NSStack < Hash
    XMLNS = 'xmlns'

    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

    # Lookup
    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

    def pop
      @parent
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
sporkmonger-sax-machine-0.1.1 lib/sax-machine/ns_stack.rb
sporkmonger-sax-machine-0.1.0 lib/sax-machine/ns_stack.rb
superfeedr-sax-machine-0.0.23 lib/sax-machine/ns_stack.rb