arjan / decorator

Function decorators for Elixir
MIT License
386 stars 21 forks source link

Decorator used with Ecto #29

Open ludwikbukowski opened 6 years ago

ludwikbukowski commented 6 years ago

Let's say I'd like to decorate_all functions that are injected by Ecto.Repo. I am struggling with this:

defmodule App.Repo do
  use Ecto.Repo, otp_app: :app
  use Decorator.Define
  @decorate_all function_result(:test)

  def function_result(add, body, _context) do
    quote do
      {unquote(add), unquote(body)}
    end
  end

(...)

Firstly, it seems not to work due to warning:

== Compilation error in file lib/app/repo.ex ==
** (ArgumentError) cannot make function __pool__/0 overridable because it was not defined
    (elixir) lib/module.ex:913: anonymous fn/2 in Module.make_overridable/2
    (stdlib) lists.erl:1338: :lists.foreach/2
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (stdlib) erl_eval.erl:228: :erl_eval.expr/5
    (stdlib) erl_eval.erl:229: :erl_eval.expr/5
    /lib/app/repo.ex:1: Decorator.Decorate.before_compile/1

Once I force filter out __pool__ function from decoration, it compiles but only few functions are decorated. Most of them are not

ludwikbukowski commented 6 years ago

Most likely thats because Ecto defines his own before_compile macro and does a lot of magic generally

arjan commented 6 years ago

Hmmm 2 incompatible forms of magic within one module, interesting...

aspett commented 6 years ago

Also appears to not work with Phoenix.View.