Search code examples
elixirphoenix-frameworkelixir-mixelixir-iex

Alias all contents of a module/namepsace in IEX


Following the advice in this question regarding how to load iex with the dependencies of the current project I was able to work with phoenix framework dependencies in a pretty productive manner. However, it gets a bit tedious having to provide the namespace of the Phoenix project for every single thing.

Rather than typing MyApp.Repo.all(MyApp.User) I was hoping to be able to do Repo.all(User). I can alias each thing individually with alias MyApp.Repo, as: Repo but is there any way to do this for everything all at once?


Solution

  • You can simply call alias MyApp.Repo instead of MyApp.Repo, as: Repo — it will use the last part of the module name.

    In Elixir 1.2 you are able to alias multiple submodules to their own names with one call: alias MyApp.{Repo, User}

    You also have the option of a .iex.exs file which you can use to set up your aliases (per the IEx docs). I wouldn't recommend it in this case as you run the risk of having a naming collision. Calling alias in an iex session is more explicit.