Class: Lutaml::Xml::TypeNamespace::Collector

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

Overview

Collects type namespace references from a model

Type namespaces are namespaces declared on attribute types via xml_namespace directive. They are collected separately from element namespaces because they have different scoping rules.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(register = nil) ⇒ Collector

Returns a new instance of Collector.



14
15
16
# File 'lib/lutaml/xml/type_namespace/collector.rb', line 14

def initialize(register = nil)
  @register = register || Lutaml::Model::Config.default_register
end

Instance Attribute Details

#registerObject (readonly)

Returns the value of attribute register.



12
13
14
# File 'lib/lutaml/xml/type_namespace/collector.rb', line 12

def register
  @register
end

Instance Method Details

#collect_from_attribute(attribute, rule, context) ⇒ Reference?

Collect type namespace references from a single attribute

Parameters:

  • attribute (Attribute)

    The attribute definition

  • rule (MappingRule)

    The mapping rule

  • context (Symbol)

    :attribute or :element

Returns:

  • (Reference, nil)

    The type namespace reference, or nil



24
25
26
27
28
29
30
# File 'lib/lutaml/xml/type_namespace/collector.rb', line 24

def collect_from_attribute(attribute, rule, context)
  return nil if rule.namespace_set?
  return nil unless attribute

  # Create reference (lazy resolution of actual namespace)
  Reference.new(attribute, rule, context)
end

#collect_from_mapping(mapping, mapper_class) ⇒ Array<Reference>

Collect all type namespace references from a mapping

Parameters:

  • mapping (Xml::Mapping)

    The XML mapping

  • mapper_class (Class)

    The model class

Returns:

  • (Array<Reference>)

    Array of type namespace references



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lutaml/xml/type_namespace/collector.rb', line 37

def collect_from_mapping(mapping, mapper_class)
  references = []

  return references unless mapper_class.is_a?(Class) &&
    mapper_class.include?(Lutaml::Model::Serialize)

  attributes = mapper_class.attributes

  # Collect from attribute rules
  mapping.attributes.each do |attr_rule|
    next unless attr_rule.attribute?
    next if attr_rule.to.nil?

    attr_def = attributes[attr_rule.to]
    next unless attr_def

    # Only collect if rule doesn't explicitly set namespace
    next if attr_rule.namespace_set?

    ref = collect_from_attribute(attr_def, attr_rule, :attribute)
    references << ref if ref
  end

  # Collect from element rules
  mapping.elements.each do |elem_rule|
    attr_def = attributes[elem_rule.to]
    next unless attr_def

    # Only collect if rule doesn't explicitly set namespace
    next if elem_rule.namespace_set?

    ref = collect_from_attribute(attr_def, elem_rule, :element)
    references << ref if ref
  end

  references
end

#resolve_references(references) ⇒ Hash<Symbol, Class>

Resolve type namespace references to namespace classes

Parameters:

  • references (Array<Reference>)

    The type namespace references

Returns:

  • (Hash<Symbol, Class>)

    Hash of context => Set of namespace classes



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lutaml/xml/type_namespace/collector.rb', line 79

def resolve_references(references)
  result = {
    attributes: Set.new,
    elements: Set.new,
  }

  references.each do |ref|
    ns_class = ref.namespace_class(@register)
    next unless ns_class

    if ref.attribute_context?
      result[:attributes] << ns_class
    else
      result[:elements] << ns_class
    end
  end

  result
end