Module: Lutaml::Xsd::Spa::Utils::ExtractEnumeration

Included in:
SchemaSerializer, XmlInstanceGenerator
Defined in:
lib/lutaml/xsd/spa/utils/extract_enumeration.rb

Instance Method Summary collapse

Instance Method Details

#extract_enumeration_default(attr) ⇒ String?

Extract enumeration default value from an attribute with inline simpleType

Parameters:

Returns:

  • (String, nil)

    Formatted enumeration default string



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
# File 'lib/lutaml/xsd/spa/utils/extract_enumeration.rb', line 13

def extract_enumeration_default(attr)
  return [nil, nil] unless attr.respond_to?(:simple_type) && attr.simple_type

  st = attr.simple_type
  if st.respond_to?(:restriction) && st.restriction
    # <xsd:simpleType>
    #   <xsd:restriction base="xsd:token">
    #     <xsd:enumeration value="A"/>
    #     <xsd:enumeration value="B"/>
    #   </xsd:restriction>
    # </xsd:simpleType>
    restriction = st.restriction
    if restriction.respond_to?(:enumeration) && restriction.enumeration
      enum_str = extract_enumeration_values(restriction.enumeration)
      return [enum_str, restriction.base]
    end
  elsif st.respond_to?(:union) && st.union
    # <xsd:simpleType>
    #   <xsd:union memberTypes="xsd:token">
    #     <xsd:simpleType>
    #       <xsd:restriction base="xsd:token">
    #         <xsd:enumeration value="A"/>
    #         <xsd:enumeration value="B"/>
    #       </xsd:restriction>
    #     </xsd:simpleType>
    #   </xsd:union>
    # </xsd:simpleType>
    union = st.union
    union_type_str = ""
    if union.respond_to?(:member_types)
      union_type_str = union.member_types
    end

    if union.respond_to?(:simple_type) && union.simple_type
      union_sub_type = union.simple_type.first
      if union_sub_type.respond_to?(:restriction) &&
          union_sub_type.restriction
        union_restriction = union_sub_type.restriction
        if union_restriction.respond_to?(:enumeration) &&
            union_restriction.enumeration
          enum_val_str = extract_enumeration_values(union_restriction
            .enumeration)

          enum_type_str = union_restriction.base
          enum_str = "union of: [ #{union_type_str}, " \
                     "[ #{union_restriction.base} " \
                     "(#{enum_val_str}) ] ]"

          return [enum_str, enum_type_str]
        end
      end
    end
  end

  [nil, nil]
end

#extract_enumeration_values(enumeration) ⇒ String

Extract enumeration values in string

Parameters:

Returns:

  • (String)


74
75
76
77
# File 'lib/lutaml/xsd/spa/utils/extract_enumeration.rb', line 74

def extract_enumeration_values(enumeration)
  values = enumeration.map { |e| "'#{e.value}'" }
  "value comes from list: #{values.join('|')}"
end