https://github.com/derailed/ex_cut
Elixir Cross Cutting function annotations
https://github.com/derailed/ex_cut
annotation cross-cut elixir
Last synced: about 1 year ago
JSON representation
Elixir Cross Cutting function annotations
- Host: GitHub
- URL: https://github.com/derailed/ex_cut
- Owner: derailed
- Created: 2017-10-19T23:26:54.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-10-30T23:22:32.000Z (over 8 years ago)
- Last Synced: 2025-03-24T21:13:38.984Z (about 1 year ago)
- Topics: annotation, cross-cut, elixir
- Language: Elixir
- Size: 57.6 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ExCut [cross-cut]
[](https://hex.pm/packages/ex_cut)
[](https://semaphoreci.com/imhotep/ex_cut)
## Motivation
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 reusable behavior without cluttering your code base.
## Documentation
[ExCut](https://hexdocs.pm/ex_cut)
## Examples
[ExCut Examples](https://github.com/derailed/ex_cut/tree/master/examples)
## Installation
Add the following dependencies to your Elixir project
```elixir
def deps do
[
{:ex_cut , "~> 0.1.0"}
]
end
```
## Using
Let's take a look at ExCut in action to add logging behavior to a set of
functions.
```elixir
defmodule Logging do
use ExCut, marker: :log, pre: :pre, post: :post
require Logger
@log level: :warn
def elvis(a, b) when is_boolean(a), do: b
@log level: :debug
def elvis(a, b) when is_atom(a), do: "#{a}--#{b}"
@log level: :info
def elvis(a, b), do: a + b
defp pre(c) , do: "> #{c.target}(#{c.args |> inspect})" |> log(c)
defp post(c, _, r), do: "< #{c.target} -> #{r}" |> log(c)
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.
---
© 2017 Imhotep Software LLC.
All materials licensed under [Apache v2.0](http://www.apache.org/licenses/LICENSE-2.0)