Sha256: 5249800db63a377205412c05110f11dcdacba7f2cbf94318de5a1eed15c6c009

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module VTD
  module Xml
    class Node
      module Attributes
        AttributeMissing = Class.new(StandardError)

        def attributes
          attributes = {}

          @auto_pilot.select_attr('*')
          while (i = @auto_pilot.iterate_attr) != -1
            attributes[string_from_index i] = string_from_index i + 1
          end

          attributes
        end

        def [](key)
          find_attribute(key)
        end

        def slice(*keys)
          Hash[*keys.flat_map { |key| [key, find_attribute(key)] }]
        end

        def fetch(*args)
          if args.length == 2
            key, default = *args
          else
            key = args.first
          end

          if item = find_attribute(key)
            return item
          end

          return yield(key) if block_given?
          return default if defined? default
          raise AttributeMissing, 'attribute not found'
        end

        private

        def find_attribute(key)
          string_from_index @nav.get_attr_val(key)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vtd-xml-0.0.3-java lib/vtd/xml/node/attributes.rb