Home

Awesome

Build Status GitHub tag Hex.pm

erlang_decorators

This code implenets decorators for erlang.

Usage

Internal

First of all you need set {parser_transform, decorators} compile option.

-compile([{parse_transform, decorators}]).

and then specify decorate attribute before funcion definition.

-decorate(DecoratorSpec).
my_function(...) -> ... .

DecoratorSpec:

External

You can also specify decorators from outside of module at compile time.

First of all you must tell compiler to use decorators parse_transform globaly. And then specify compile decorate option, which describes how to decorate modules functions. Syntax:

{decorate,
    [
     {Module :: module(),
     [
      {{Fun :: atom(), Arity :: non_neg_integer() | '*'} | '*',
       [DecoratorSpec, ...]},
      ...
     ],
     ...
    ]}

Examples

Internal


-module(my_module).

-compile([{parse_transform, decorators}]).

%% decorator
-export([
         log_result/2
        ]).

%% Decorated function
-export([
         foo/1
        ]).

log_result(Fun, Args) ->
    Result = Fun(Args),
    io:format("Result is: ~p", [Result]),
    Result.

-decorate({?MODULE, log_result}).
foo(Bar) ->
    Bar*100.

External

rebar.config

{erl_opts,
 [
  {parse_transform, decorators},
  {decorate,
   [
    {my_module1,
     [
      {{fun1, 0}, [{my_decorators, decorator1}]},
      {{fun2, 0}, [
                   {my_decorators, decorator1},
                   {my_decorators, decorator2}
                  ]},
     ]},
    {my_module2,
     [
      {{fun2, 0}, [
                   {my_decorators, decorator3, [tag]},
                   {my_decorators, decorator4, [tag], verbose}
                  ]}
     ]}
   ]},
   ...
 ]}.