Class: Lutaml::Model::Schema::XmlCompiler::SpecBuilder::Members

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb

Overview

Builds the leaf member specs that live inside generated classes: Attribute / Element (both surface as Definitions::Attribute with different kind: values), Sequence, Choice, GroupImport, plus the TypeRef wrapper.

Holds @parent (SpecBuilder) so it can:

- look up @parent.attributes / @parent.elements for refs
- write anonymous simple/complex types into @parent's hashes
- delegate complex-type building back to @parent for nested
anonymous types

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Members

Returns a new instance of Members.



19
20
21
# File 'lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb', line 19

def initialize(parent)
  @parent = parent
end

Instance Method Details

#build_attribute(attr) ⇒ Object

----- Attributes ------------------------------------------------



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb', line 25

def build_attribute(attr)
  return resolve_attribute_ref(attr) if attr.ref && !attr.name

  Definitions::Attribute.new(
    name: Utils.snake_case(attr.name),
    type: build_type_ref(resolve_attribute_type(attr)),
    xml_name: attr.name,
    kind: :attribute,
    default: attr.default,
  )
end

#build_choice(choice) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb', line 77

def build_choice(choice)
  alternatives = ElementOrder.resolved(choice).filter_map { |item| build_member(item) }
  Definitions::Choice.new(
    alternatives: alternatives,
    header: choice_header(choice),
  )
end

#build_element(element) ⇒ Object

----- Elements --------------------------------------------------



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb', line 51

def build_element(element)
  return resolve_element_ref(element) if element.ref && !element.name

  Definitions::Attribute.new(
    name: Utils.snake_case(element.name),
    type: build_type_ref(resolve_element_type(element)),
    xml_name: element.name,
    kind: :element,
    collection: collection_from_occurs(element.min_occurs, element.max_occurs),
    default: element.default,
    render_default: !element.default.nil?,
    render_empty: element_required?(element.min_occurs),
  )
end

#build_group_member(group) ⇒ Object

A <xs:group ref="..."/> appearing inside a sequence/choice becomes an inline import directive. Anonymous in-place groups (no name, no ref) are flattened by the complex-type walker before they reach this method.



89
90
91
92
93
94
95
# File 'lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb', line 89

def build_group_member(group)
  return nil if group.ref.nil?

  Definitions::GroupImport.new(
    name: Utils.snake_case(Utils.last_of_split(group.ref)),
  )
end

#build_sequence(sequence) ⇒ Object

----- Sequence / Choice -----------------------------------------



68
69
70
71
72
73
74
75
# File 'lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb', line 68

def build_sequence(sequence)
  members = ElementOrder.resolved(sequence).filter_map do |item|
    next if item.is_a?(Lutaml::Xml::Schema::Xsd::Any)

    build_member(item)
  end
  Definitions::Sequence.new(members: members)
end

#build_top_level_attribute(item, kind:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb', line 37

def build_top_level_attribute(item, kind:)
  return item unless item.respond_to?(:name)

  type_str = top_level_type_str(item)
  Definitions::Attribute.new(
    name: Utils.snake_case(item.name.to_s),
    type: build_type_ref(type_str || "string"),
    xml_name: item.name.to_s,
    kind: kind,
  )
end

#build_type_ref(raw_type) ⇒ Object

----- TypeRef ---------------------------------------------------



99
100
101
102
103
104
105
# File 'lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb', line 99

def build_type_ref(raw_type)
  return Definitions::TypeRef.new(kind: :symbol, value: "string") if raw_type.nil?
  return Definitions::TypeRef.new(kind: :w3c, value: raw_type) if w3c_type?(raw_type)

  local = Utils.last_of_split(raw_type)
  Definitions::TypeRef.new(kind: :symbol, value: Utils.snake_case(local))
end