Class: Lutaml::Xml::ParsedNamespaceSet
- Inherits:
-
Object
- Object
- Lutaml::Xml::ParsedNamespaceSet
- Defined in:
- lib/lutaml/xml/parsed_namespace_set.rb
Overview
Collection of parsed namespace declarations, providing OOP access and lookups.
Supports lookups by prefix, by URI (canonical or alias), and by original URI. Backward-compatible with plain hash storage via #to_input_namespaces_hash and ParsedNamespaceSet.from_hash.
Class Method Summary collapse
-
.from_hash(hash, declared_at_path: []) ⇒ ParsedNamespaceSet
Build from a plain hash (backward compat for Document.input_namespaces).
Instance Method Summary collapse
-
#add(declaration) ⇒ self
Add a declaration to the set.
- #each {|declaration| ... } ⇒ self
- #empty? ⇒ Boolean
-
#find(prefix, uri) ⇒ ParsedNamespaceDeclaration?
Declaration with a specific (prefix, uri) combination.
-
#for_prefix(prefix) ⇒ Array<ParsedNamespaceDeclaration>
All declarations for a given prefix.
-
#for_uri(uri) ⇒ Array<ParsedNamespaceDeclaration>
All declarations for a given URI (matches canonical or alias).
-
#initialize(declarations = []) ⇒ ParsedNamespaceSet
constructor
A new instance of ParsedNamespaceSet.
-
#multiple_prefixes_for_uri?(uri) ⇒ Boolean
Check if a URI has multiple prefix variants (doubly-defined namespace).
-
#prefixes ⇒ Array<String>
All unique prefixes (excludes :default).
-
#root_declarations ⇒ Array<ParsedNamespaceDeclaration>
All declarations at root level.
- #size ⇒ Integer
-
#to_input_namespaces_hash ⇒ Hash
Serialize to Hash for backward compatibility with Document.input_namespaces.
-
#uris ⇒ Array<String>
All unique effective URIs.
Constructor Details
#initialize(declarations = []) ⇒ ParsedNamespaceSet
Returns a new instance of ParsedNamespaceSet.
21 22 23 24 25 26 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 21 def initialize(declarations = []) @by_prefix = {} # prefix string => [ParsedNamespaceDeclaration] @by_uri = {} # effective_uri => [ParsedNamespaceDeclaration] @by_original_uri = {} # original_uri => ParsedNamespaceDeclaration declarations.each { |d| add(d) } end |
Class Method Details
.from_hash(hash, declared_at_path: []) ⇒ ParsedNamespaceSet
Build from a plain hash (backward compat for Document.input_namespaces)
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 116 def self.from_hash(hash, declared_at_path: []) decls = hash.map do |prefix_key, config| prefix = prefix_key == :default ? nil : prefix_key.to_s ParsedNamespaceDeclaration.new( uri: config[:uri], prefix: prefix, format: config[:format] || (prefix ? :prefix : :default), declared_at_path: declared_at_path, canonical_uri: config[:canonical_uri], original_uri: config[:original_uri], ) end new(decls) end |
Instance Method Details
#add(declaration) ⇒ self
Add a declaration to the set
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 31 def add(declaration) key = declaration.prefix || :default @by_prefix[key] ||= [] @by_prefix[key] << declaration @by_uri[declaration.effective_uri] ||= [] @by_uri[declaration.effective_uri] << declaration if declaration.original_uri @by_original_uri[declaration.original_uri] = declaration end self end |
#each {|declaration| ... } ⇒ self
138 139 140 141 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 138 def each(&) @by_prefix.values.flatten.uniq.each(&) self end |
#empty? ⇒ Boolean
132 133 134 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 132 def empty? @by_prefix.empty? end |
#find(prefix, uri) ⇒ ParsedNamespaceDeclaration?
Declaration with a specific (prefix, uri) combination
64 65 66 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 64 def find(prefix, uri) for_prefix(prefix).find { |d| d.effective_uri == uri } end |
#for_prefix(prefix) ⇒ Array<ParsedNamespaceDeclaration>
All declarations for a given prefix
49 50 51 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 49 def for_prefix(prefix) @by_prefix[prefix || :default] || [] end |
#for_uri(uri) ⇒ Array<ParsedNamespaceDeclaration>
All declarations for a given URI (matches canonical or alias)
56 57 58 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 56 def for_uri(uri) @by_uri[uri] || [] end |
#multiple_prefixes_for_uri?(uri) ⇒ Boolean
Check if a URI has multiple prefix variants (doubly-defined namespace)
89 90 91 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 89 def multiple_prefixes_for_uri?(uri) for_uri(uri).reject(&:default_namespace?).size > 1 end |
#prefixes ⇒ Array<String>
All unique prefixes (excludes :default)
82 83 84 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 82 def prefixes @by_prefix.keys.reject { |k| k == :default } end |
#root_declarations ⇒ Array<ParsedNamespaceDeclaration>
All declarations at root level
70 71 72 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 70 def root_declarations @by_prefix.values.flatten.select { |d| d.declared_at_path.empty? } end |
#size ⇒ Integer
144 145 146 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 144 def size @by_prefix.values.flatten.uniq.size end |
#to_input_namespaces_hash ⇒ Hash
Serialize to Hash for backward compatibility with Document.input_namespaces
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 95 def to_input_namespaces_hash result = {} @by_prefix.each do |prefix, decls| decls.each do |d| key = prefix == :default ? :default : prefix result[key] = { uri: d.uri, prefix: d.prefix, format: d.format, canonical_uri: d.canonical_uri, original_uri: d.original_uri, } end end result end |
#uris ⇒ Array<String>
All unique effective URIs
76 77 78 |
# File 'lib/lutaml/xml/parsed_namespace_set.rb', line 76 def uris @by_uri.keys end |