class WsdlMapper::Dom::Namespaces

The Namespaces class stores XML namespaces (URLs) along with the prefix/abbreviation used. e.g. :ns1 => 'example.org/mynamespace'

In addition to that, it allows generation of unique prefixes for URLs not present in this collection. See {#prefix_for}

Attributes

default[RW]

The default namespace (that without a prefix on node names)

Public Class Methods

for(hash) click to toggle source

Converts a hash of prefix => url pairs to a new {Namespaces} collection.

@param [Hash{String => String}] @return [Namespaces] New collection of namespaces

# File lib/wsdl_mapper/dom/namespaces.rb, line 79
def self.for(hash)
  ns = new
  hash.each do |prefix, url|
    ns[prefix] = url
  end
  ns
end
new(prefix: 'ns') click to toggle source

Initializes a new collection of namespaces.

@param [String] prefix Prefix to use for generated prefixes.

# File lib/wsdl_mapper/dom/namespaces.rb, line 14
def initialize(prefix: 'ns')
  @namespaces = {}
  @default = nil
  @i = 0
  @prefix = prefix
end

Public Instance Methods

[](prefix)
Alias for: get
[]=(prefix, url)
Alias for: set
each(&block) click to toggle source

Enumerable implementation, returns the key value pairs of prefix => url, beginning with the default (if set), where the prefix then is `nil`.

# File lib/wsdl_mapper/dom/namespaces.rb, line 64
def each(&block)
  enum = Enumerator.new do |y|
    y << [nil, default] if default
    @namespaces.each do |prefix, url|
      y << [prefix, url]
    end
  end

  block_given? ? enum.each(&block) : enum
end
get(prefix) click to toggle source

Gets a namespace / URL for a given prefix

@param [String, Symbol] prefix Prefix to get the URL for @return [String] URL / namespace if exists, `nil` otherwise

# File lib/wsdl_mapper/dom/namespaces.rb, line 38
def get(prefix)
  @namespaces[prefix.to_s]
end
Also aliased as: []
prefix_for(url) click to toggle source

Gets a prefix for the given URL / namespace. If the `url` does not exist in this collection, a unique prefix is generated automatically. If `url` matches the {#default} namespace, `nil` is returned. If `url` is `nil`, `nil` is returned as well.

@param [String] url URL / namespace @return [String] Prefix for the given `url`

# File lib/wsdl_mapper/dom/namespaces.rb, line 49
def prefix_for(url)
  return nil if url.nil?
  return nil if url == @default

  prefix = @namespaces.key(url)
  return prefix if prefix

  prefix = @prefix + @i.to_s
  @i += 1
  set(prefix, url)
  prefix
end
set(prefix, url) click to toggle source

Add / Set a specific prefix for a given URL. If a prefix already exists in this collection, it will be overwritten.

@param [String, Symbol] prefix Prefix to set for the URL @param [String] url URL / namespace to assign to this prefix

# File lib/wsdl_mapper/dom/namespaces.rb, line 29
def set(prefix, url)
  @namespaces[prefix.to_s] = url
end
Also aliased as: []=