Alias all contents of a module/namepsace in IEX
Asked Answered
P

1

10

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?

Perorate answered 29/12, 2015 at 4:3 Comment(0)
S
13

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.

Seicento answered 29/12, 2015 at 8:24 Comment(1)
That looks like it will work great, thanks! I think aliasing the the phoenix models in the iex.exs file on a per project basis will get me exactly what I need without risking too many collisions.Perorate

© 2022 - 2024 — McMap. All rights reserved.