Class: GrapeOAS::Exporter::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_oas/exporter/registry.rb

Overview

Registry for managing schema exporters for different OpenAPI versions. Allows third-party gems to register custom exporters for new formats.

Examples:

Registering a custom exporter with single alias

GrapeOAS.exporters.register(MyCustomExporter, as: :custom)

Registering with multiple aliases

GrapeOAS.exporters.register(OAS30Schema, as: [:oas3, :oas30])

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



15
16
17
# File 'lib/grape_oas/exporter/registry.rb', line 15

def initialize
  @exporters = {}
end

Instance Method Details

#clearself

Clears all registered exporters.

Returns:

  • (self)


76
77
78
79
# File 'lib/grape_oas/exporter/registry.rb', line 76

def clear
  @exporters.clear
  self
end

#for(schema_type) ⇒ Class

Finds the exporter class for a given schema type.

Parameters:

  • schema_type (Symbol)

    The schema type

Returns:

  • (Class)

    The exporter class

Raises:

  • (ArgumentError)

    if no exporter is registered for the type



44
45
46
47
48
49
# File 'lib/grape_oas/exporter/registry.rb', line 44

def for(schema_type)
  exporter = @exporters[schema_type]
  raise ArgumentError, "Unsupported schema type: #{schema_type}" unless exporter

  exporter
end

#register(exporter_class, as:) ⇒ self

Registers an exporter class for one or more schema types.

Parameters:

  • exporter_class (Class)

    The exporter class to register

  • as (Symbol, Array<Symbol>)

    The schema type identifier(s)

Returns:

  • (self)


24
25
26
27
28
# File 'lib/grape_oas/exporter/registry.rb', line 24

def register(exporter_class, as:)
  schema_types = Array(as)
  schema_types.each { |type| @exporters[type] = exporter_class }
  self
end

#registered?(schema_type) ⇒ Boolean

Checks if an exporter is registered for the given schema type.

Parameters:

  • schema_type (Symbol)

    The schema type to check

Returns:

  • (Boolean)


55
56
57
# File 'lib/grape_oas/exporter/registry.rb', line 55

def registered?(schema_type)
  @exporters.key?(schema_type)
end

#schema_typesArray<Symbol>

Returns all registered schema types.

Returns:

  • (Array<Symbol>)


62
63
64
# File 'lib/grape_oas/exporter/registry.rb', line 62

def schema_types
  @exporters.keys
end

#sizeInteger

Returns the number of registered exporters.

Returns:

  • (Integer)


69
70
71
# File 'lib/grape_oas/exporter/registry.rb', line 69

def size
  @exporters.size
end

#unregister(*schema_types) ⇒ self

Unregisters an exporter for one or more schema types.

Parameters:

  • schema_types (Symbol, Array<Symbol>)

    The schema type(s) to remove

Returns:

  • (self)


34
35
36
37
# File 'lib/grape_oas/exporter/registry.rb', line 34

def unregister(*schema_types)
  schema_types.flatten.each { |type| @exporters.delete(type) }
  self
end