Class: Lutaml::Xsd::Spa::SchemaSerializer Abstract

Inherits:
Object
  • Object
show all
Includes:
Utils::ExtractEnumeration
Defined in:
lib/lutaml/xsd/spa/schema_serializer.rb

Overview

This class is abstract.

Subclass and override template methods to customize serialization

Base class for schema serializers (Template Method Pattern)

Defines the overall algorithm for serializing XSD schema repositories into a format suitable for SPA documentation. Subclasses can override specific steps to customize the serialization process.

The serialization process follows these steps:

  1. Build metadata

  2. Serialize all schemas

  3. Build search index

  4. Assemble final structure

Examples:

Using the serializer

serializer = JsonSchemaSerializer.new(repository)
data = serializer.serialize
json = serializer.to_json

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::ExtractEnumeration

#extract_enumeration_default, #extract_enumeration_values

Constructor Details

#initialize(repository_or_package, config = {}) ⇒ SchemaSerializer

Initialize schema serializer

Parameters:



40
41
42
43
44
45
46
47
48
49
# File 'lib/lutaml/xsd/spa/schema_serializer.rb', line 40

def initialize(repository_or_package, config = {})
  if repository_or_package.is_a?(SchemaRepositoryPackage)
    @package = repository_or_package
    @repository = repository_or_package.repository
  else
    @repository = repository_or_package
    @package = nil
  end
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



34
35
36
# File 'lib/lutaml/xsd/spa/schema_serializer.rb', line 34

def config
  @config
end

#packageObject (readonly)

Returns the value of attribute package.



34
35
36
# File 'lib/lutaml/xsd/spa/schema_serializer.rb', line 34

def package
  @package
end

#repositoryObject (readonly)

Returns the value of attribute repository.



34
35
36
# File 'lib/lutaml/xsd/spa/schema_serializer.rb', line 34

def repository
  @repository
end

Instance Method Details

#serializeHash

Serialize repository to data structure (template method)

This method defines the overall algorithm for serialization. Subclasses can override individual steps as needed.

Returns:

  • (Hash)

    Serialized data structure



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lutaml/xsd/spa/schema_serializer.rb', line 57

def serialize
   = 
  # Convert SpaMetadata model to hash for JSON serialization
  # SpaMetadata.to_hash returns string keys, but we need symbol keys for compatibility
   = if .respond_to?(:to_hash)
                    hash = .to_hash
                    # Convert string keys to symbols for backward compatibility
                    hash.to_h do |k, v|
                      [k.is_a?(String) ? k.to_sym : k, v]
                    end
                  elsif .respond_to?(:to_h)
                    .to_h
                  else
                    
                  end
  {
    metadata: ,
    schemas: serialize_schemas,
    namespaces: build_namespaces,
    index: build_index,
  }
end

#to_json(pretty: true) ⇒ String

Convert serialized data to JSON string

Parameters:

  • pretty (Boolean) (defaults to: true)

    Whether to pretty-print JSON

Returns:

  • (String)

    JSON string



84
85
86
87
# File 'lib/lutaml/xsd/spa/schema_serializer.rb', line 84

def to_json(pretty: true)
  data = serialize
  pretty ? JSON.pretty_generate(data) : JSON.generate(data)
end