arjan / decorator

Function decorators for Elixir
MIT License
385 stars 20 forks source link

[Question] How to decorate all functions in a module? #15

Closed aspett closed 6 years ago

aspett commented 6 years ago

I'm trying to use this library to decorate every function in a module. My current strategy for doing this is using an on_definition hook, and put_attribute'ing the required parameters. This appears to be working nearly. It has some trouble with multi-clause functions; especially with function headers.

After banging my head against this for a few hours, I thought it might just be better to ask if there's an easier way to decorate every function. Thanks!

arjan commented 6 years ago

This is an interesting question. I think this could be implemented using a decorate_all attribute:

defmodule Foo do
      use MyDecorator
      @decorate_all some_function(:foo)
end

Internally I would check for this attribute and decorate all defs based on it.

aspett commented 6 years ago

This would be very helpful for me. Honestly, I struggle to get my head around this codebase, though.

ludwikbukowski commented 6 years ago

++

aspett commented 6 years ago

@ludwikbukowski I made progress here: https://github.com/arjan/decorator/pull/16 but it breaks function headers at the moment. Not sure how to fix that yet

arjan commented 6 years ago

@aspett I merged an implementation similar to yours. I believe the issue with function headers will be dealt with in #18, so separate from this.