Search documentation of Metastatic

# Metastatic.Adapter.Registry (Metastatic v0.13.2)

View Source

Registry for language adapters.

Provides a centralized registry for managing language adapters that implement the `Metastatic.Adapter` behaviour. Adapters can be registered dynamically or discovered automatically from the application.

## Meta-Modeling Context

The registry maintains mappings between:

- Languages (atoms like `:python`, `:javascript`) → Adapter modules
- File extensions (strings like `".py"`) → Languages

This enables automatic language detection and adapter selection for M1 ↔ M2 transformations.

## Usage

    # Register an adapter
    Registry.register(:python, Metastatic.Adapters.Python)

    # Get adapter for a language
    {:ok, adapter} = Registry.get(:python)

    # Detect language from filename
    {:ok, language} = Registry.detect_language("script.py")

    # List all registered adapters
    adapters = Registry.list()

## Summary

### Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

detect_language(filename)

Detect language from filename based on extension.

get(language)

Get the adapter module for a language.

list()

List all registered adapters.

register(language, adapter)

Register a language adapter.

registered?(language)

Check if a language is registered.

start_link(opts \\ [])

Start the registry.

unregister(language)

Unregister a language adapter.

validate_all()

Validate that all registered adapters are still valid.

## Functions

# child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# detect_language(filename)

    @spec detect_language(String.t()) :: {:ok, atom()} | {:error, :unknown_extension}

Detect language from filename based on extension.

Uses the registered adapters' file extensions to determine the language.

## Examples

    iex> Registry.detect_language("script.py")
    {:ok, :python}

    iex> Registry.detect_language("app.js")
    {:ok, :javascript}

    iex> Registry.detect_language("unknown.xyz")
    {:error, :unknown_extension}

# get(language)

    @spec get(atom()) :: {:ok, module()} | {:error, :not_found}

Get the adapter module for a language.

## Examples

    iex> Registry.get(:python)
    {:ok, Metastatic.Adapters.Python}

    iex> Registry.get(:unknown)
    {:error, :not_found}

# list()

    @spec list() :: %{required(atom()) => module()}

List all registered adapters.

Returns a map of language atoms to adapter modules.

## Examples

    iex> Registry.list()
    %{python: Metastatic.Adapters.Python, javascript: Metastatic.Adapters.JavaScript}

# register(language, adapter)

    @spec register(atom(), module()) :: :ok | {:error, :invalid_adapter}

Register a language adapter.

## Parameters

- `language` - Language identifier (e.g., `:python`, `:javascript`)
- `adapter` - Module implementing `Metastatic.Adapter` behaviour

## Examples

    iex> Registry.register(:python, Metastatic.Adapters.Python)
    :ok

    iex> Registry.register(:python, InvalidModule)
    {:error, :invalid_adapter}

# registered?(language)

    @spec registered?(atom()) :: boolean()

Check if a language is registered.

## Examples

    iex> Registry.registered?(:python)
    true

    iex> Registry.registered?(:unknown)
    false

# start_link(opts \\ [])

    @spec start_link(keyword()) :: GenServer.on_start()

Start the registry.

This is typically called by the application supervisor.

# unregister(language)

    @spec unregister(atom()) :: :ok

Unregister a language adapter.

## Examples

    iex> Registry.unregister(:python)
    :ok

# validate_all()

    @spec validate_all() :: {:ok, []} | {:error, [{atom(), String.t()}]}

Validate that all registered adapters are still valid.

Returns a list of invalid adapters (if any).

## Examples

    iex> Registry.validate_all()
    {:ok, []}

    iex> Registry.validate_all()
    {:error, [python: "Missing required callback: parse/1"]}