AshCommanded.Commanded.Transformers.GenerateEventHandlerModules (AshCommanded v0.1.0)

View Source

Generates event handler modules based on the event_handlers defined in the DSL.

For each resource with event_handlers, this transformer will generate handler modules that subscribe to the events and execute the specified actions or functions.

This transformer should run after the event module transformer.

Example

Given a resource with several event_handlers, this transformer will generate:

defmodule MyApp.EventHandlers.UserNotificationHandler do
  @moduledoc "General purpose event handler for User events"
  
  use Commanded.Event.Handler,
    application: MyApp.CommandedApplication,
    name: "MyApp.EventHandlers.UserNotificationHandler"
    
  def handle(%MyApp.Events.UserRegistered{} = event, _metadata) do
    # Call the action or execute the function defined in the DSL
    MyApp.Notifications.send_welcome_email(event.email)
    :ok
  end
  
  # More handlers for other events...
end

Summary

Functions

Specifies that this transformer should run after the event module transformer.

Callback implementation for Spark.Dsl.Transformer.before?/1.

Builds a map of event names to their corresponding handlers.

Builds the module name for an event handler.

Builds the AST (Abstract Syntax Tree) for an event handler module.

Groups handlers by their name or autogenerated name based on resource.

Transforms the DSL state to generate event handler modules.

Functions

after?(arg1)

Specifies that this transformer should run after the event module transformer.

after_compile?()

Callback implementation for Spark.Dsl.Transformer.after_compile?/0.

before?(_)

Callback implementation for Spark.Dsl.Transformer.before?/1.

build_event_handlers_map(handlers, events)

Builds a map of event names to their corresponding handlers.

Examples

iex> build_event_handlers_map(handlers, events)
%{user_registered: [handler1, handler2], email_changed: [handler3]}

build_handler_module(handler_name, resource_name, app_prefix)

Builds the module name for an event handler.

Examples

iex> build_handler_module(:notification_handler, "User", MyApp)
MyApp.EventHandlers.UserNotificationHandler

build_handler_module_ast(resource_module, resource_name, handlers, event_handlers_map, event_modules)

Builds the AST (Abstract Syntax Tree) for an event handler module.

Examples

iex> build_handler_module_ast(MyApp.User, "User", handlers, event_map, event_modules)
{:__block__, [], [{:@, [...], [{:moduledoc, [...], [...]}]}, ...]}

group_handlers_by_name(handlers)

Groups handlers by their name or autogenerated name based on resource.

Examples

iex> group_handlers_by_name(handlers)
%{notification_handler: [handler1, handler2], integration_handler: [handler3]}

transform(dsl_state)

Transforms the DSL state to generate event handler modules.

Examples

iex> transform(dsl_state)
{:ok, updated_dsl_state}