heathen
heathen

Reputation: 300

How to modify existing module behaviour in Elixir?

Is it possible to add a new functionality to the module which doesn't have __using__/1 function?

For example, I would like to add get_meta_by_key function to Phoenix.Tracker. If I go with

defmodule MyApp.MyTracker do
  use Phoenix.Tracker

  def get_meta_by_key(..., topic, key)
    ...
  end

  handle_call(:get_meta_by_key, ..., state)
    ...
  end
end

then I get ** (UndefinedFunctionError) undefined function Phoenix.Tracker.__using__/1

I would like to have distributed in-memory key-value store to keep some temporary values but can't figure out how to do it without reinventing the wheel and without using Phoenix.Tracker.list function and then dealing with the full list on the caller side.

I'm pretty new in Elixir/Phoenix, so please excuse me if my question is stupid.

Upvotes: 0

Views: 393

Answers (1)

Sasha Fonseca
Sasha Fonseca

Reputation: 2313

You can write your own using macros with using. This is metaprogramming related and I highly recommend looking at Metaprogramming Elixir book for this. You will also need to use quote and unquote for it.

You may also refer to this

Upvotes: 1

Related Questions