Class: WsdlMapper::Dom::Namespaces

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/wsdl_mapper/dom/namespaces.rb

Overview

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

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix: 'ns') ⇒ Namespaces

Initializes a new collection of namespaces.

Parameters:

  • prefix (String) (defaults to: 'ns')

    Prefix to use for generated prefixes.



14
15
16
17
18
19
# File 'lib/wsdl_mapper/dom/namespaces.rb', line 14

def initialize(prefix: 'ns')
  @namespaces = {}
  @default = nil
  @i = 0
  @prefix = prefix
end

Instance Attribute Details

#defaultObject

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



22
23
24
# File 'lib/wsdl_mapper/dom/namespaces.rb', line 22

def default
  @default
end

Class Method Details

.for(hash) ⇒ Namespaces

Converts a hash of prefix => url pairs to a new WsdlMapper::Dom::Namespaces collection.

Parameters:

  • (Hash{String => String})

Returns:



79
80
81
82
83
84
85
# 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

Instance Method Details

#each(&block) ⇒ Object

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



64
65
66
67
68
69
70
71
72
73
# 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) ⇒ String Also known as: []

Gets a namespace / URL for a given prefix

Parameters:

  • prefix (String, Symbol)

    Prefix to get the URL for

Returns:

  • (String)

    URL / namespace if exists, nil otherwise



38
39
40
# File 'lib/wsdl_mapper/dom/namespaces.rb', line 38

def get(prefix)
  @namespaces[prefix.to_s]
end

#prefix_for(url) ⇒ String

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.

Parameters:

  • url (String)

    URL / namespace

Returns:

  • (String)

    Prefix for the given url



49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object Also known as: []=

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

Parameters:

  • prefix (String, Symbol)

    Prefix to set for the URL

  • url (String)

    URL / namespace to assign to this prefix



29
30
31
# File 'lib/wsdl_mapper/dom/namespaces.rb', line 29

def set(prefix, url)
  @namespaces[prefix.to_s] = url
end