ex_cut v0.1.0 ExCut View Source
ExCut defines an annotation construct that wraps a regular function and enable it to be decorated with a cross-cutting concern. This provide a clean mechanism to inject resusable behavior without cluttering your code base.
Let’s take a look at ExCut in action to add logging behavior to a set of functions.
defmodule Blee do
use ExCut, marker: :log, pre: :pre_log, post: :post_log
require Logger
# Basic annotation setting a log level
@log level: :info
def elvis(a, b), do: a + b
# You can also override module pre or post within an annotation
@log level: :debug, pre: :cust_pre_log
def elvis(a, b) when is_binary(a), do: a <> b
@log level: :warm
def elvis(a, b) when is_boolean(a), do: b
defp pre_log(ctx) do
">>> #{ctx.target} with args #{ctx.args |> inspect}"
|> log(ctx)
end
defp post_log(ctx, _pre, _res) do
"<<< #{ctx.target} with args #{ctx.args |> inspect}"
|> log(ctx)
end
defp cust_pre_log(ctx) do
"[CUSTOM!] >>> #{ctx.target} with args #{ctx.args |> inspect}"
|> log(ctx)
end
defp log(m, ctx) do
ctx.meta[:level]
|> case do
:warn -> m |> Logger.warn
:debug -> m |> Logger.debug
_ -> m |> Logger.info
end
end
end
ExCut provisions an ExCut.Context
with call details and metadata
that comes from the annotation. You can leverage this information in
your cross-cutting functions.
Link to this section Summary
Functions
Defines an annotation to enable cross-cutting concerns to be injected before the function is called and after the function exits. This macro defines a new function that overrides the original function and decorates the call. The annotated function is generated at compile time
Generates a new cross-cutting function with optional before and after hooks that calls the original function. Pre and Post hooks can be defined at the module level or overriden on a per function basis
Flags functions that have been annotated for further processing during the compilation phase
Link to this section Functions
Defines an annotation to enable cross-cutting concerns to be injected before the function is called and after the function exits. This macro defines a new function that overrides the original function and decorates the call. The annotated function is generated at compile time.
Generates a new cross-cutting function with optional before and after hooks that calls the original function. Pre and Post hooks can be defined at the module level or overriden on a per function basis.
Flags functions that have been annotated for further processing during the compilation phase