alexcastano

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 :slight_smile:

Most Liked

aseigo

aseigo

So, there is the super function that does what you want with behaviours:

https://hexdocs.pm/elixir/Kernel.SpecialForms.html#super/1

You can also use the using and import directives to extend modules in nicely generic ways:

https://yos.io/2016/04/24/mixins-in-elixir/

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 :slight_smile:

aseigo

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 https://github.com/stavro/arc 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

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.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

We're in Beta

About us Mission Statement