alexcastano
Plugin system in Elixir
Hello everyone,
I read some Elixir books and I did some small toy projects with Elixir. My mind is still too object oriented programming but I want to start creating “real code”, specifically, I’d like to create shrinerb in Elixir. It is an awesome library that I use in Ruby and I think it could be cool to learn some Elixir.
My question is about the integrated plugin system. The author explained it very well in this blog post Briefly, it consists some base classes with a few methods which do very little. Then the plugins overwrite those methods adding some functionality and calling super, this way they don’t conflict each other. The user can select the plugins he needs and only those plugins are loaded. I think it is very elegant.
I don’t find a way to translate this to Elixir. The only solution I can think it is to create a “middleware engine” for each method of each class, so the plugins can inject their code safely. Two ways to do this are creating something similar to Tesla middleware or using defoverridable. However, I actually see this an overkill task. I don’t have enough experience to determinate if they are Elixir-ish solutions.
What do you think? Are good solutions? Or am I translating OOP to a functional language and it does not fit? In this case, what would be your solution?
Thank you very much for your time ![]()
Most Liked Responses
aseigo
So, there is the super function that does what you want with behaviours:
You can also use the using and import directives to extend modules in nicely generic ways:
So you can achieve what you are looking for that way .. buuuuuut ..
I have to say that to me, calling “super” is generally a code smell (not that it is always “wrong”, as in “worst thing one could do”, though ..). Functions are cheap in Elixir, and instead of calling a magical super version of the functions, why not call the actual function you are after? The behaviour can define upload and delete, and the Uploader module can provide a set of helper functions. This allows each implementation of the behaviour to explicitly pick which functions to use. This opens doors like being able to freely compose the set of functionality that makes sense for that specific module .. and if there are common composition patterns, put those compositions in a function that can be called directly .. so you can get explictness, DRY, and flexibility all in one package.
The inheritance straight-jacket of “use your inheritance hierarchy as a code path” encouraged OO is often not the most expressive or flexible approach .. functions are our friends, and they can be called by name ![]()
aseigo
If I am understanding your query correct, I think what you want to look into are Behaviours:
https://elixirschool.com/lessons/advanced/behaviours/
.. also.. while I 100% understand the “write something I want/need to learn ..” you may also want to take a look at GitHub - stavro/arc: 📎 Flexible file upload and attachment library for Elixir · GitHub if you haven’t already come across it. If nothing else, it may give inspiration and/or a starting point …
Cheers, and enjoy your new journey in the land of Elixir!
peerreynders
I don’t think this is what you’re after - but just in case - eplugin has a totally different approach to plugins.
There is a dedicated gen_server process that is responsible compiling the plugins and managing their particulars in ETS tables.








