Class: Lutaml::Xml::NamespaceUsage

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(namespace_class) ⇒ NamespaceUsage

Initialize namespace usage tracking

Parameters:

  • namespace_class (Class)

    XmlNamespace class



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_prefixObject

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_useObject (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_classObject (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_inObject (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_prefixObject

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

Parameters:

  • key (Symbol)

    Accessor key

Returns:

  • (Object)

    Corresponding attribute value



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)

Returns:

  • (Boolean)


94
95
96
# File 'lib/lutaml/xml/namespace_usage.rb', line 94

def empty?
  @used_in.empty? && @children_use.empty?
end

#inspectString

String representation for debugging

Returns:

  • (String)


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

#keyString

Get namespace key for lookups

Returns:

  • (String)


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

Parameters:

  • child_name (Symbol)

    Child attribute name



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

Parameters:

  • context (Symbol)

    Context: :elements, :attributes, or :content



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

Parameters:

Returns:

  • (self)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


43
44
45
# File 'lib/lutaml/xml/namespace_usage.rb', line 43

def used_in_elements?
  @used_in.include?(:elements)
end