Elixir Function Decorator

A function decorator macro for Elixir. Used mainly for adding log statements to the function calls.

This project is based mainly on:

Motivation

All I did was use the solution provided by the mentioned above, add some minor refactoring and adjustments for my needs and package it as an helper module.

This was a learning project getting into Elixir macros field.

For getting into Elixir Macros, you are encourgae to read Saša Jurić 's excellent Elixir macro articles series

State

Installation

If available in Hex, the package can be installed as:

  1. Add function_decorating to your list of dependencies in mix.exs:

    def deps do

     [{:function_decorating, "~> 0.0.1"}]

    end

  2. Ensure function_decorating is started before your application:

    def application do

     [applications: [:function_decorating]]

    end

Usage

Decorating in dev with log decorator.

defmodule User do
  use FunctionDecorating
  decorate_fn_with(LogDecorator)

  def say(word) do
    word
  end
end
iex>User.say("hello")
#PID<0.86.0> [x] Elixir.User.say(["hello"]) -> "hello"
"hello"

Default usage is for Mix.env == :dev only. To override it:

defmodule User do
  use FunctionDecorating mix_envs: [:test]
  decorate_fn_with(LogDecorator)

  def say(word) do
    word
  end
end
iex >Mix.env
:test

iex >User.say("hello")
#PID<0.86.0> [x] Elixir.User.say(["hello"]) -> "hello"
"hello"