Class: WsdlMapper::Dom::Namespaces
- Inherits:
-
Object
- Object
- WsdlMapper::Dom::Namespaces
- 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
-
#default ⇒ Object
The default namespace (that without a prefix on node names).
Class Method Summary collapse
-
.for(hash) ⇒ Namespaces
Converts a hash of prefix => url pairs to a new Namespaces collection.
Instance Method Summary collapse
-
#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. -
#get(prefix) ⇒ String
(also: #[])
Gets a namespace / URL for a given prefix.
-
#initialize(prefix: 'ns') ⇒ Namespaces
constructor
Initializes a new collection of namespaces.
-
#prefix_for(url) ⇒ String
Gets a prefix for the given URL / namespace.
-
#set(prefix, url) ⇒ Object
(also: #[]=)
Add / Set a specific prefix for a given URL.
Constructor Details
#initialize(prefix: 'ns') ⇒ Namespaces
Initializes a new collection of namespaces.
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
#default ⇒ Object
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.
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
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.
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.
29 30 31 |
# File 'lib/wsdl_mapper/dom/namespaces.rb', line 29 def set(prefix, url) @namespaces[prefix.to_s] = url end |