Class: Synthra::Export::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/synthra/export/base.rb

Overview

This class is abstract.

Subclass and implement #export to create an exporter

Base class for all exporters

Provides common functionality for exporting schemas to different formats.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, registry: nil, **options) ⇒ Base

Create a new exporter

Parameters:

  • schema (Schema)

    the schema to export

  • registry (Registry) (defaults to: nil)

    the registry for resolving references

  • options (Hash)

    export options



28
29
30
31
32
# File 'lib/synthra/export/base.rb', line 28

def initialize(schema, registry: nil, **options)
  @schema = schema
  @registry = registry
  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

Returns export options.

Returns:

  • (Hash)

    export options



20
21
22
# File 'lib/synthra/export/base.rb', line 20

def options
  @options
end

#registryRegistry (readonly)

Returns the registry for resolving references.

Returns:

  • (Registry)

    the registry for resolving references



17
18
19
# File 'lib/synthra/export/base.rb', line 17

def registry
  @registry
end

#schemaSchema (readonly)

Returns the schema to export.

Returns:

  • (Schema)

    the schema to export



14
15
16
# File 'lib/synthra/export/base.rb', line 14

def schema
  @schema
end

Instance Method Details

#all_fieldsArray<Field> (protected)

Get all fields including from nested schemas

Returns:

  • (Array<Field>)

    all fields



58
59
60
# File 'lib/synthra/export/base.rb', line 58

def all_fields
  schema.fields
end

#exportString

This method is abstract.

Export the schema to the target format

Returns:

  • (String)

    the exported content

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/synthra/export/base.rb', line 39

def export
  raise NotImplementedError, "Subclasses must implement #export"
end

#get_schema(type_name) ⇒ Schema? (protected)

Get referenced schema

Parameters:

  • type_name (String)

    the type name

Returns:



76
77
78
79
80
# File 'lib/synthra/export/base.rb', line 76

def get_schema(type_name)
  registry&.schema(type_name)
rescue KeyError
  nil # schema not found
end

#save(path) ⇒ void

This method returns an undefined value.

Save export to a file

Parameters:

  • path (String)

    file path



48
49
50
# File 'lib/synthra/export/base.rb', line 48

def save(path)
  File.write(path, export)
end

#schema_reference?(type_name) ⇒ Boolean (protected)

Check if a type is a schema reference

Parameters:

  • type_name (String)

    the type name

Returns:

  • (Boolean)


67
68
69
# File 'lib/synthra/export/base.rb', line 67

def schema_reference?(type_name)
  registry&.schema?(type_name) || false
end