marcelotto / behaviour_reflection

Get all modules implementing an Elixir behaviour

Home Page:https://hex.pm/packages/behaviour_reflection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Behaviour.Reflection

Travis Hex.pm

Get all modules implementing an Elixir behaviour.

See this article for an analysis of the problem, the circumstances for when to use this library, how it works and its caveats.

Installation

The Hex package can be installed as usual, by adding behaviour_reflection to your list of dependencies in mix.exs:

def deps do
  [
    {:behaviour_reflection, "~> 0.1"}
  ]
end

Usage

Let's say you have a behaviour and a number of modules implementing it:

defmodule MyBehaviour do
  @callback fun :: String.t
end

defmodule Foo do
  @behaviour MyBehaviour
  def fun(), do: "foo"
end

defmodule Bar do
  @behaviour MyBehaviour
  def fun(), do: "bar"
end

At runtime you can retrieve all modules implementing the behaviour like this:

iex> Behaviour.Reflection.impls(MyBehaviour)
[Foo, Bar]

Documentation can be found at https://hexdocs.pm/behaviour_reflection.

About

Get all modules implementing an Elixir behaviour

https://hex.pm/packages/behaviour_reflection

License:MIT License


Languages

Language:Elixir 100.0%