Class: Lutaml::Xml::Schema::Xsd::ComplexType

Inherits:
Base show all
Defined in:
lib/lutaml/xml/schema/xsd/complex_type.rb

Constant Summary collapse

DIRECT_CHILD_ELEMENTS_EXCEPTION =
%w[
  AttributeGroup
  AnyAttribute
  Annotation
  Attribute
].freeze

Constants inherited from Base

Base::ELEMENT_ORDER_IGNORABLE, Base::XML_DECLARATION_REGEX

Constants included from Model::Serialize

Model::Serialize::DEFAULT_VALUE_MAP, Model::Serialize::INTERNAL_ATTRIBUTES, Model::Serialize::LAZY_EMPTY_COLLECTION

Instance Attribute Summary

Attributes included from Model::Serialize

#lutaml_parent, #lutaml_register, #lutaml_root

Instance Method Summary collapse

Methods inherited from Base

#all?, #annotation?, #any?, #assign_root!, #attribute?, #attribute_group?, #choice?, #complex_content?, #element?, #max_occurrences, #min_occurrences, #resolved_element_order, #sequence?, #simple_content?, #target_prefix, #to_formatted_xml, #unresolvable_items

Methods included from Model::Serialize

#attr_value, #attribute_exist?, #extract_register_id, included, #init_deserialization_state, #initialize, #key_exist?, #key_value, #method_missing, #prepare_instance_format_options, #pretty_print_instance_variables, register_format_mapping_method, register_from_format_method, register_to_format_method, #respond_to_missing?, #to_format, #to_yaml_hash, #using_default?, #using_default_for, #validate_attribute!, #validate_root_mapping!, #value_map, #value_set_for

Methods included from Model::Liquefiable

included, #to_liquid

Methods included from Model::Validation

#format_element_sequences, #order_names, #validate, #validate!, #validate_helper, #validate_sequence!

Methods included from Model::ComparableModel

#already_compared?, #attributes_hash, #calculate_hash, #comparison_key, #eql?, #hash, included, #same_class?

Methods included from Model::Serialize::Builder

#initialize, #mixed_content?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Lutaml::Model::Serialize

Instance Method Details

#attribute_elements(array = []) ⇒ Object

Flatten attributes declared directly on the complex type and those brought in through groups and content extensions.



80
81
82
83
84
85
86
# File 'lib/lutaml/xml/schema/xsd/complex_type.rb', line 80

def attribute_elements(array = [])
  array.concat(attribute)
  attribute_group.each { |group| group.attribute_elements(array) }
  simple_content&.attribute_elements(array)
  complex_content&.attribute_elements(array)
  array
end

#child_elements(array = []) ⇒ Object

Walk nested content containers and collect the element nodes they expose in document order.



104
105
106
107
108
109
110
111
112
113
# File 'lib/lutaml/xml/schema/xsd/complex_type.rb', line 104

def child_elements(array = [])
  resolved_element_order&.each do |child|
    if child.is_a?(Element)
      array << child
    elsif child.respond_to?(:child_elements)
      child.child_elements(array)
    end
  end
  array
end

#direct_child_elements(array = [], except: DIRECT_CHILD_ELEMENTS_EXCEPTION) ⇒ Object

Return structural children while skipping attributes and other non-element content wrappers listed in the exception set.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lutaml/xml/schema/xsd/complex_type.rb', line 90

def direct_child_elements(array = [],
except: DIRECT_CHILD_ELEMENTS_EXCEPTION)
  resolved_element_order&.each do |child|
    next if except.any? do |klass|
      child.class.name.include?("::#{klass}")
    end

    array << child if child.resolved_element_order&.any?
  end
  array
end

#elementsArray<Element>

Get elements from the primary content model (sequence, choice, or all).

Returns:

  • (Array<Element>)

    Elements exposed by the active content model



141
142
143
144
145
146
147
# File 'lib/lutaml/xml/schema/xsd/complex_type.rb', line 141

def elements
  return sequence.element if sequence.respond_to?(:element)
  return choice.element if choice.respond_to?(:element)
  return all.element if all.respond_to?(:element)

  []
end

#find_elements_used(element_name) ⇒ Object

Check whether a referenced element name is used anywhere within this complex type’s nested content model.



117
118
119
120
121
122
123
124
125
# File 'lib/lutaml/xml/schema/xsd/complex_type.rb', line 117

def find_elements_used(element_name)
  resolved_element_order&.any? do |child|
    if child.is_a?(Element)
      reference_matches?(element_name, child.ref || child.name)
    elsif child.respond_to?(:find_elements_used)
      child.find_elements_used(element_name)
    end
  end || false
end

#find_used_by(object) ⇒ Object

Recursively inspect another object to see whether it references this complex type through nested attribute groups.



129
130
131
132
133
134
135
136
137
# File 'lib/lutaml/xml/schema/xsd/complex_type.rb', line 129

def find_used_by(object)
  object&.resolved_element_order&.any? do |child|
    if child.is_a?(AttributeGroup)
      child.ref == name
    else
      find_used_by(child)
    end
  end || false
end

#used_byObject

Return root-level elements and nested child elements that refer to this complex type by name.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lutaml/xml/schema/xsd/complex_type.rb', line 66

def used_by
  root_complex_types = xsd_root.complex_type.reject do |complex_type|
    complex_type == self
  end
  raw_elements = xsd_root.group.flat_map(&:child_elements)
  raw_elements.concat(xsd_root.element)
  raw_elements.concat(root_complex_types.flat_map(&:child_elements))
  raw_elements.select do |element|
    reference_matches?(name, element.type)
  end
end