Class: Lutaml::Xsd::SerializedSchema

Inherits:
Model::Serializable
  • Object
show all
Defined in:
lib/lutaml/xsd/serialized_schema.rb

Overview

Represents a serialized schema for storage in packages

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_schema(file_path, schema) ⇒ SerializedSchema

Create from a parsed Schema object

Parameters:

  • file_path (String)

    Original file path

  • schema (Schema)

    Parsed schema object

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lutaml/xsd/serialized_schema.rb', line 23

def self.from_schema(file_path, schema)
  # Serialize schema to JSON instead of YAML to avoid circular reference issues
  # We'll store the essential data that can be reconstructed
  schema_hash = {
    target_namespace: schema.target_namespace,
    element_form_default: schema.element_form_default,
    attribute_form_default: schema.attribute_form_default,
    version: schema.version,
    simple_types: serialize_types(schema.simple_type),
    complex_types: serialize_types(schema.complex_type),
    elements: serialize_elements(schema.element),
    attribute_groups: serialize_attribute_groups(schema.attribute_group),
    groups: serialize_groups(schema.group),
  }

  new(
    file_path: file_path,
    target_namespace: schema.target_namespace,
    schema_data: JSON.generate(schema_hash),
  )
end

.serialize_attribute_groups(groups) ⇒ Object

Serialize attribute groups



143
144
145
146
147
# File 'lib/lutaml/xsd/serialized_schema.rb', line 143

def self.serialize_attribute_groups(groups)
  return [] unless groups

  groups.map { |g| { name: g.name } }
end

.serialize_elements(elements) ⇒ Object

Serialize elements



119
120
121
122
123
124
125
126
127
128
# File 'lib/lutaml/xsd/serialized_schema.rb', line 119

def self.serialize_elements(elements)
  return [] unless elements

  elements.map do |elem|
    {
      name: elem.name,
      type: elem.type,
    }
  end
end

.serialize_groups(groups) ⇒ Object

Serialize groups



161
162
163
164
165
# File 'lib/lutaml/xsd/serialized_schema.rb', line 161

def self.serialize_groups(groups)
  return [] unless groups

  groups.map { |g| { name: g.name } }
end

.serialize_types(types) ⇒ Object

Serialize type definitions to a hash



78
79
80
81
82
83
84
85
86
87
# File 'lib/lutaml/xsd/serialized_schema.rb', line 78

def self.serialize_types(types)
  return [] unless types

  types.map do |type|
    {
      name: type.name,
      class: type.class.name,
    }
  end
end

Instance Method Details

#to_schemaSchema

Deserialize back to a Schema object

Returns:



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/xsd/serialized_schema.rb', line 47

def to_schema
  data = JSON.parse(schema_data)

  # Create a minimal Schema object with the essential data
  schema = Schema.new(
    target_namespace: data["target_namespace"],
    element_form_default: data["element_form_default"],
    attribute_form_default: data["attribute_form_default"],
    version: data["version"],
  )

  # Reconstruct collections
  schema.instance_variable_set(:@simple_type,
                               deserialize_types(data["simple_types"],
                                                 :simple_type))
  schema.instance_variable_set(:@complex_type,
                               deserialize_types(data["complex_types"],
                                                 :complex_type))
  schema.instance_variable_set(:@element,
                               deserialize_elements(data["elements"]))
  schema.instance_variable_set(:@attribute_group,
                               deserialize_attribute_groups(data["attribute_groups"]))
  schema.instance_variable_set(:@group,
                               deserialize_groups(data["groups"]))

  schema
end