chocolatedonut

chocolatedonut

`alias` all modules of a project, as a good baseline for a project-root's .iex.exs

How can I alias all modules of a project?

Mix.Project.apps_paths 
|> Map.keys
|> Enum.flat_map(&:application.get_key(&1, :modules) |> elem(1)) 

I’m eyeing Code.eval_string, but not sure how to use it. I could continue above pipeline with, say

|> Enum.map(&IO.inspect "alias #{&1}") 
|> Enum.join("\n")

but still lost.

(Above is a bit half-baked, but would be grateful if anyone immediately sees the solution.)

Most Liked

Nicd

Nicd

Why do you want to alias all modules? What are you aiming to do?

Kintull

Kintull

I came up with this code block that allows me to load all project aliases for the quick terminal usage

defmodule Console do
  defmacro aliases(expression) do
      {:ok, modules} = :application.get_key(:<YOUR_APP>, :modules)
  
      modules =
        for module <- modules do
          quote do
            alias unquote(module)
          end
        end
  
      quote do
        unquote(modules)
        unquote(expression)
      end
  end
end

So now I can execute something like this:

iex(10)> require Console
iex(11)> Console.aliases Repo.all(Room)
[
  %Tarragon.Battles.Room{
    __meta__: #Ecto.Schema.Metadata<:loaded, "battle_rooms">,
    id: 1,
...

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement