Class: Lutaml::Xml::NamespaceNeeds

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/xml/namespace_needs.rb

Overview

Collects namespace usage data during collection phase Replaces schema-less needs hash with proper OOP structure

This class maintains MECE responsibility: it only stores and organizes namespace needs data. It does NOT make decisions or build XML.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNamespaceNeeds

Returns a new instance of NamespaceNeeds.



16
17
18
19
20
21
22
23
24
25
# File 'lib/lutaml/xml/namespace_needs.rb', line 16

def initialize
  @namespaces = {} # Hash<String, NamespaceUsage>
  @children = {} # Hash<Symbol, NamespaceNeeds>
  @type_namespaces = {} # Hash<Symbol, Class>
  @type_namespace_classes = Set.new # Set<Class>
  @type_attribute_namespaces = Set.new # Set<Class>
  @type_element_namespaces = Set.new # Set<Class>
  @type_refs = [] # Array<TypeNamespace::Reference>
  @namespace_scope_configs = [] # Array<NamespaceScopeConfig>
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



11
12
13
# File 'lib/lutaml/xml/namespace_needs.rb', line 11

def children
  @children
end

#namespace_scope_configsObject (readonly)

Returns the value of attribute namespace_scope_configs.



11
12
13
# File 'lib/lutaml/xml/namespace_needs.rb', line 11

def namespace_scope_configs
  @namespace_scope_configs
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



11
12
13
# File 'lib/lutaml/xml/namespace_needs.rb', line 11

def namespaces
  @namespaces
end

#type_attribute_namespacesObject (readonly)

Returns the value of attribute type_attribute_namespaces.



11
12
13
# File 'lib/lutaml/xml/namespace_needs.rb', line 11

def type_attribute_namespaces
  @type_attribute_namespaces
end

#type_element_namespacesObject (readonly)

Returns the value of attribute type_element_namespaces.



11
12
13
# File 'lib/lutaml/xml/namespace_needs.rb', line 11

def type_element_namespaces
  @type_element_namespaces
end

#type_namespace_classesObject (readonly)

Returns the value of attribute type_namespace_classes.



11
12
13
# File 'lib/lutaml/xml/namespace_needs.rb', line 11

def type_namespace_classes
  @type_namespace_classes
end

#type_namespacesObject (readonly)

Returns the value of attribute type_namespaces.



11
12
13
# File 'lib/lutaml/xml/namespace_needs.rb', line 11

def type_namespaces
  @type_namespaces
end

#type_refsObject (readonly)

Returns the value of attribute type_refs.



11
12
13
# File 'lib/lutaml/xml/namespace_needs.rb', line 11

def type_refs
  @type_refs
end

Instance Method Details

#add_child(name, child_needs) ⇒ Object

Add child needs

Parameters:

  • name (Symbol)

    Child attribute name

  • child_needs (NamespaceNeeds)

    Child’s namespace needs



91
92
93
94
95
96
97
98
# File 'lib/lutaml/xml/namespace_needs.rb', line 91

def add_child(name, child_needs)
  unless child_needs.is_a?(NamespaceNeeds)
    raise ArgumentError,
          "Expected NamespaceNeeds, got #{child_needs.class}"
  end

  @children[name] = child_needs
end

#add_namespace(key, usage) ⇒ Object

Add namespace usage for a specific namespace

Parameters:

  • key (String)

    Namespace key (from namespace_class.to_key)

  • usage (NamespaceUsage)

    Usage information



30
31
32
33
34
35
36
# File 'lib/lutaml/xml/namespace_needs.rb', line 30

def add_namespace(key, usage)
  unless usage.is_a?(NamespaceUsage)
    raise ArgumentError, "Expected NamespaceUsage, got #{usage.class}"
  end

  @namespaces[key] = usage
end

#add_namespace_scope_config(config) ⇒ Object

Add namespace scope configuration

Parameters:



79
80
81
82
83
84
85
86
# File 'lib/lutaml/xml/namespace_needs.rb', line 79

def add_namespace_scope_config(config)
  unless config.is_a?(NamespaceScopeConfig)
    raise ArgumentError,
          "Expected NamespaceScopeConfig, got #{config.class}"
  end

  @namespace_scope_configs << config
end

#add_type_attribute_namespace(ns_class) ⇒ Object

Add type attribute namespace to tracking set

Parameters:

  • ns_class (Class)

    XmlNamespace class



48
49
50
51
# File 'lib/lutaml/xml/namespace_needs.rb', line 48

def add_type_attribute_namespace(ns_class)
  @type_attribute_namespaces << ns_class
  @type_namespace_classes << ns_class
end

#add_type_element_namespace(ns_class) ⇒ Object

Add type element namespace to tracking set

Parameters:

  • ns_class (Class)

    XmlNamespace class



55
56
57
58
# File 'lib/lutaml/xml/namespace_needs.rb', line 55

def add_type_element_namespace(ns_class)
  @type_element_namespaces << ns_class
  @type_namespace_classes << ns_class
end

#add_type_namespace(attr_name, ns_class) ⇒ Object

Track a type namespace for a specific attribute

Parameters:

  • attr_name (Symbol)

    Attribute name

  • ns_class (Class)

    XmlNamespace class



41
42
43
44
# File 'lib/lutaml/xml/namespace_needs.rb', line 41

def add_type_namespace(attr_name, ns_class)
  @type_namespaces[attr_name] = ns_class
  @type_namespace_classes << ns_class
end

#add_type_ref(reference) ⇒ Object

Add a type reference for lazy resolution

Parameters:



62
63
64
65
66
67
68
69
# File 'lib/lutaml/xml/namespace_needs.rb', line 62

def add_type_ref(reference)
  unless reference.is_a?(TypeNamespace::Reference)
    raise ArgumentError,
          "Expected TypeNamespace::Reference, got #{reference.class}"
  end

  @type_refs << reference
end

#all_namespace_classesSet<Class>

Get all namespace classes (from usage and type namespaces)

Returns:

  • (Set<Class>)


182
183
184
185
# File 'lib/lutaml/xml/namespace_needs.rb', line 182

def all_namespace_classes
  namespace_classes = Set.new(@namespaces.values.map(&:namespace_class))
  namespace_classes.merge(@type_namespace_classes)
end

#child(name) ⇒ NamespaceNeeds?

Get child needs by name

Parameters:

  • name (Symbol)

    Child attribute name

Returns:



167
168
169
# File 'lib/lutaml/xml/namespace_needs.rb', line 167

def child(name)
  @children[name]
end

#clear_type_refsObject

Clear type references after resolution Used by TypeNamespaceResolver to prevent reprocessing



73
74
75
# File 'lib/lutaml/xml/namespace_needs.rb', line 73

def clear_type_refs
  @type_refs.clear
end

#empty?Boolean

Check if needs are empty

Returns:

  • (Boolean)


149
150
151
152
153
154
155
# File 'lib/lutaml/xml/namespace_needs.rb', line 149

def empty?
  @namespaces.empty? &&
    @children.empty? &&
    @type_refs.empty? &&
    @type_namespaces.empty? &&
    @namespace_scope_configs.empty?
end

#merge(other) ⇒ self

Merge another NamespaceNeeds into this one

Parameters:

Returns:

  • (self)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/lutaml/xml/namespace_needs.rb', line 103

def merge(other)
  unless other.is_a?(NamespaceNeeds)
    raise ArgumentError, "Expected NamespaceNeeds, got #{other.class}"
  end

  # Merge namespaces
  other.namespaces.each do |key, usage|
    if @namespaces.key?(key)
      @namespaces[key].merge(usage)
    else
      @namespaces[key] = usage
    end
  end

  # Merge children
  other.children.each do |name, child_needs|
    if @children.key?(name)
      @children[name].merge(child_needs)
    else
      @children[name] = child_needs
    end
  end

  # Merge type namespaces
  @type_namespaces.merge!(other.type_namespaces)
  @type_namespace_classes.merge(other.type_namespace_classes)
  @type_attribute_namespaces.merge(other.type_attribute_namespaces)
  @type_element_namespaces.merge(other.type_element_namespaces)

  # Merge type refs
  @type_refs.concat(other.type_refs)

  # Merge namespace scope configs (avoiding duplicates)
  other.namespace_scope_configs.each do |config|
    unless @namespace_scope_configs.any? do |c|
      c.namespace_class == config.namespace_class
    end
      @namespace_scope_configs << config
    end
  end

  self
end

#namespace(key) ⇒ NamespaceUsage?

Get namespace usage by key

Parameters:

  • key (String)

    Namespace key

Returns:



160
161
162
# File 'lib/lutaml/xml/namespace_needs.rb', line 160

def namespace(key)
  @namespaces[key]
end

#scope_config_for(ns_class) ⇒ NamespaceScopeConfig?

Check if a namespace is in scope configuration

Parameters:

  • ns_class (Class)

    XmlNamespace class

Returns:



174
175
176
177
178
# File 'lib/lutaml/xml/namespace_needs.rb', line 174

def scope_config_for(ns_class)
  @namespace_scope_configs.find do |config|
    config.namespace_class == ns_class
  end
end

#validate!Object

Validate internal consistency

Raises:

  • (RuntimeError)

    if inconsistent state detected



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/lutaml/xml/namespace_needs.rb', line 189

def validate!
  # Type attribute and element namespaces should be mutually exclusive
  overlap = @type_attribute_namespaces & @type_element_namespaces
  unless overlap.empty?
    raise "Type namespaces appear in both attribute and element contexts: #{overlap.to_a}"
  end

  # All type namespace classes should be in type_namespace_classes
  @type_attribute_namespaces.each do |ns_class|
    unless @type_namespace_classes.include?(ns_class)
      raise "Type attribute namespace #{ns_class} not in type_namespace_classes"
    end
  end

  @type_element_namespaces.each do |ns_class|
    unless @type_namespace_classes.include?(ns_class)
      raise "Type element namespace #{ns_class} not in type_namespace_classes"
    end
  end

  true
end