AshDispatch.Resource.Info (AshDispatch v0.5.1)

View Source

Introspection helpers for AshDispatch.Resource.

Provides functions to query event configurations from resources at runtime.

Usage

# Get all events for a resource
events = AshDispatch.Resource.Info.events(MyApp.Orders.ProductOrder)

# Get a specific event by name
event = AshDispatch.Resource.Info.event(MyApp.Orders.ProductOrder, :created)

# Get event options
event_id = AshDispatch.Resource.Info.event_id(event)
trigger_on = AshDispatch.Resource.Info.trigger_on(event)
channels = AshDispatch.Resource.Info.channels(event)

Generated Functions

This module uses Spark.InfoGenerator to automatically generate helper functions for querying the dispatch section and its entities.

Resource-level:

  • events(resource) - Returns all events defined in the resource

Event entity getters:

  • name(event) - Get event name
  • trigger_on(event) - Get action names that trigger this event
  • module(event) - Get callback module (if any)
  • event_id(event) - Get event ID
  • domain(event) - Get event domain
  • load(event) - Get relationships to preload
  • channels(event) - Get channel configurations
  • content(event) - Get content configuration
  • metadata(event) - Get metadata configuration

Examples

# Find events triggered by specific action
iex> events = AshDispatch.Resource.Info.events(ProductOrder)
iex> Enum.filter(events, fn event ->
...>   :create in List.wrap(AshDispatch.Resource.Info.trigger_on(event))
...> end)
[%AshDispatch.Resource.Dsl.Event{name: :created, ...}]

# Get all event IDs for a resource
iex> events = AshDispatch.Resource.Info.events(ProductOrder)
iex> Enum.map(events, &AshDispatch.Resource.Info.event_id/1)
["product_order.created", "product_order.cancelled"]

# Check if event has callback module
iex> event = AshDispatch.Resource.Info.event(ProductOrder, :created)
iex> if AshDispatch.Resource.Info.module(event) do
...>   "Uses callback module"
...> else
...>   "Uses inline config"
...> end
"Uses inline config"

Summary

Functions

Get a specific counter by name.

Get all counters defined in a resource.

dispatch DSL entities

Check if a resource has the AshDispatch.Resource extension.

Get entity_changes configuration for a resource, if any.

Check if entity change broadcasting is enabled for a resource.

Get a specific event by name.

Count total events for a resource.

Get all event IDs for a resource.

Get all events defined in a resource.

Get all events that trigger on a specific action.

Get all events that use callback modules.

Get all events that use inline configuration.

Get resource_meta configuration for a resource, if any.

Functions

counter(resource, name)

Get a specific counter by name.

Parameters

  • resource - The resource module
  • name - The counter name (atom)

Returns

  • %AshDispatch.Resource.Dsl.Counter{} if found
  • nil if not found

Examples

iex> AshDispatch.Resource.Info.counter(ProductOrder, :pending_orders_counter)
%AshDispatch.Resource.Dsl.Counter{name: :pending_orders_counter, ...}

counters(resource)

Get all counters defined in a resource.

Parameters

  • resource - The resource module

Returns

List of %AshDispatch.Resource.Dsl.Counter{} structs

Examples

iex> AshDispatch.Resource.Info.counters(ProductOrder)
[%AshDispatch.Resource.Dsl.Counter{name: :pending_orders_counter, ...}]

dispatch(dsl_or_extended)

@spec dispatch(dsl_or_extended :: module() | map()) :: [struct()]

dispatch DSL entities

dispatch_enabled?(resource)

Check if a resource has the AshDispatch.Resource extension.

Parameters

  • resource - The resource module

Returns

  • true if resource uses AshDispatch.Resource
  • false otherwise

Examples

iex> AshDispatch.Resource.Info.dispatch_enabled?(ProductOrder)
true

iex> AshDispatch.Resource.Info.dispatch_enabled?(SomeOtherResource)
false

entity_changes(resource)

Get entity_changes configuration for a resource, if any.

Returns the %AshDispatch.Resource.Dsl.EntityChanges{} struct if entity_changes true is defined in the dispatch section, or nil.

entity_changes_enabled?(resource)

Check if entity change broadcasting is enabled for a resource.

event(resource, name)

Get a specific event by name.

Parameters

  • resource - The resource module
  • name - The event name (atom)

Returns

  • %AshDispatch.Resource.Dsl.Event{} if found
  • nil if not found

Examples

iex> AshDispatch.Resource.Info.event(ProductOrder, :created)
%AshDispatch.Resource.Dsl.Event{name: :created, ...}

iex> AshDispatch.Resource.Info.event(ProductOrder, :nonexistent)
nil

event_count(resource)

Count total events for a resource.

Parameters

  • resource - The resource module

Returns

Integer count of events

Examples

iex> AshDispatch.Resource.Info.event_count(ProductOrder)
3

event_ids(resource)

Get all event IDs for a resource.

Parameters

  • resource - The resource module

Returns

List of event ID strings

Examples

iex> AshDispatch.Resource.Info.event_ids(ProductOrder)
["product_order.created", "product_order.cancelled", "product_order.shipped"]

events(resource)

Get all events defined in a resource.

Parameters

  • resource - The resource module

Returns

List of %AshDispatch.Resource.Dsl.Event{} structs

Examples

iex> AshDispatch.Resource.Info.events(ProductOrder)
[%AshDispatch.Resource.Dsl.Event{name: :created, ...}]

events_for_action(resource, action_name)

Get all events that trigger on a specific action.

Parameters

  • resource - The resource module
  • action_name - The action name (atom)

Returns

List of %AshDispatch.Resource.Dsl.Event{} structs

Examples

iex> AshDispatch.Resource.Info.events_for_action(ProductOrder, :create)
[%AshDispatch.Resource.Dsl.Event{name: :created, trigger_on: :create}]

iex> AshDispatch.Resource.Info.events_for_action(ProductOrder, :update)
[]

events_with_modules(resource)

Get all events that use callback modules.

Parameters

  • resource - The resource module

Returns

List of %AshDispatch.Resource.Dsl.Event{} structs that have a module defined

Examples

iex> AshDispatch.Resource.Info.events_with_modules(ProductOrder)
[%AshDispatch.Resource.Dsl.Event{name: :complex_event, module: MyApp.Events.Complex}]

inline_events(resource)

Get all events that use inline configuration.

Parameters

  • resource - The resource module

Returns

List of %AshDispatch.Resource.Dsl.Event{} structs that use inline config

Examples

iex> AshDispatch.Resource.Info.inline_events(ProductOrder)
[%AshDispatch.Resource.Dsl.Event{name: :created, module: nil, channels: [...]}]

resource_meta(resource)

Get resource_meta configuration for a resource, if any.

Returns the %AshDispatch.Resource.Dsl.ResourceMeta{} struct if resource_meta is defined in the dispatch section, or nil.