Exaop - a minimal library for aspect-oriented programming

Exaop is a minimal library for aspect-oriented programming.

In most cases, you may not need to use AOP pattern. But for some specific complex application workflows, hope it helps :slight_smile:

GitHub: https://github.com/nobrick/exaop/
Hex: https://hexdocs.pm/exaop/api-reference.html

Example:

defmodule Wallet do
  use Exaop
  require Logger

  # Decouple cross-cutting concerns to make workflows simple and self-documenting.
  set :config, [:max_allowed_amount, :fee_rate]
  set :accounts

  check :amount, guard: :positive
  check :amount, guard: {:lt_or_eq, :max_allowed_amount}
  check :recipient, :not_equal_to_sender
  check Wallet.AML

  set :fee
  check :balance

  # A function injected by explicitly calling __inject__/2 generated by Exaop.
  def transfer(%{from: _, to: _, amount: _} = payload) do
    info
    |> __inject__(%{})
    |> handle_inject(payload)
  end

  defp handle_inject({:error, _} = error, _payload) do
    error
  end

  defp handle_inject(_acc, _payload) do
    Wallet.Core.transfer!(acc, payload)
  end

  # Then implement your callbacks required by the macros, eg. check_amount/3.
  # View the full code snippet in GitHub for details.
end

All suggestion, criticism and feedback are welcome.

3 Likes