Class: Lutaml::Xml::NamespaceUsage
- Inherits:
-
Object
- Object
- Lutaml::Xml::NamespaceUsage
- Defined in:
- lib/lutaml/xml/namespace_usage.rb
Overview
Tracks how a namespace is used (elements, attributes, etc.) Replaces used_in:, children_use:, children_need_prefix: hash
This class maintains MECE responsibility: it only tracks namespace usage. It does NOT make decisions about format or location.
Instance Attribute Summary collapse
-
#children_need_prefix ⇒ Object
Returns the value of attribute children_need_prefix.
-
#children_use ⇒ Object
readonly
Returns the value of attribute children_use.
-
#namespace_class ⇒ Object
readonly
Returns the value of attribute namespace_class.
-
#used_in ⇒ Object
readonly
Returns the value of attribute used_in.
-
#used_prefix ⇒ Object
Returns the value of attribute used_prefix.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Backward compatibility: allow hash-style access Maps old hash keys to new OOP attributes.
-
#empty? ⇒ Boolean
Check if empty (no usage tracked).
-
#initialize(namespace_class) ⇒ NamespaceUsage
constructor
Initialize namespace usage tracking.
-
#inspect ⇒ String
String representation for debugging.
-
#key ⇒ String
Get namespace key for lookups.
-
#mark_child_use(child_name) ⇒ Object
Mark namespace as used by a child.
-
#mark_used_in(context) ⇒ Object
Mark namespace as used in a specific context.
-
#merge(other) ⇒ self
Merge another usage into this one.
-
#used_by_children? ⇒ Boolean
Check if namespace is used by any children.
-
#used_in_attributes? ⇒ Boolean
Check if namespace is used in attributes.
-
#used_in_content? ⇒ Boolean
Check if namespace is used in content.
-
#used_in_elements? ⇒ Boolean
Check if namespace is used in elements.
Constructor Details
#initialize(namespace_class) ⇒ NamespaceUsage
Initialize namespace usage tracking
16 17 18 19 20 21 22 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 16 def initialize(namespace_class) @namespace_class = namespace_class @used_in = Set.new # Set of contexts: :elements, :attributes, :content @children_use = Set.new # Set of child attribute names @children_need_prefix = false @used_prefix = nil end |
Instance Attribute Details
#children_need_prefix ⇒ Object
Returns the value of attribute children_need_prefix.
12 13 14 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 12 def children_need_prefix @children_need_prefix end |
#children_use ⇒ Object (readonly)
Returns the value of attribute children_use.
11 12 13 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 11 def children_use @children_use end |
#namespace_class ⇒ Object (readonly)
Returns the value of attribute namespace_class.
11 12 13 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 11 def namespace_class @namespace_class end |
#used_in ⇒ Object (readonly)
Returns the value of attribute used_in.
11 12 13 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 11 def used_in @used_in end |
#used_prefix ⇒ Object
Returns the value of attribute used_prefix.
12 13 14 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 12 def used_prefix @used_prefix end |
Instance Method Details
#[](key) ⇒ Object
Backward compatibility: allow hash-style access Maps old hash keys to new OOP attributes
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 102 def [](key) case key when :ns_object, :namespace_class then @namespace_class when :used_in then @used_in when :children_use then @children_use when :children_need_prefix then @children_need_prefix when :used_prefix then @used_prefix else raise KeyError, "Unknown key: #{key.inspect}" end end |
#empty? ⇒ Boolean
Check if empty (no usage tracked)
94 95 96 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 94 def empty? @used_in.empty? && @children_use.empty? end |
#inspect ⇒ String
String representation for debugging
116 117 118 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 116 def inspect "#<NamespaceUsage #{@namespace_class} used_in=#{@used_in.to_a} children=#{@children_use.to_a}>" end |
#key ⇒ String
Get namespace key for lookups
88 89 90 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 88 def key @namespace_class.to_key end |
#mark_child_use(child_name) ⇒ Object
Mark namespace as used by a child
37 38 39 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 37 def mark_child_use(child_name) @children_use << child_name end |
#mark_used_in(context) ⇒ Object
Mark namespace as used in a specific context
26 27 28 29 30 31 32 33 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 26 def mark_used_in(context) unless %i[elements attributes content].include?(context) raise ArgumentError, "Invalid context: #{context}. Must be :elements, :attributes, or :content" end @used_in << context end |
#merge(other) ⇒ self
Merge another usage into this one
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 68 def merge(other) unless other.is_a?(NamespaceUsage) raise ArgumentError, "Expected NamespaceUsage, got #{other.class}" end unless other.namespace_class == @namespace_class raise ArgumentError, "Cannot merge usage for different namespaces: #{@namespace_class} != #{other.namespace_class}" end @used_in.merge(other.used_in) @children_use.merge(other.children_use) @children_need_prefix ||= other.children_need_prefix @used_prefix ||= other.used_prefix self end |
#used_by_children? ⇒ Boolean
Check if namespace is used by any children
61 62 63 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 61 def used_by_children? !@children_use.empty? end |
#used_in_attributes? ⇒ Boolean
Check if namespace is used in attributes
49 50 51 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 49 def used_in_attributes? @used_in.include?(:attributes) end |
#used_in_content? ⇒ Boolean
Check if namespace is used in content
55 56 57 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 55 def used_in_content? @used_in.include?(:content) end |
#used_in_elements? ⇒ Boolean
Check if namespace is used in elements
43 44 45 |
# File 'lib/lutaml/xml/namespace_usage.rb', line 43 def used_in_elements? @used_in.include?(:elements) end |