Skip to content

Instantly share code, notes, and snippets.

@falood
Last active March 11, 2017 15:47
Show Gist options
  • Save falood/8f316c87e21660f3c10b116098377a5f to your computer and use it in GitHub Desktop.
Save falood/8f316c87e21660f3c10b116098377a5f to your computer and use it in GitHub Desktop.
Macro in maru step by step: Router
get :path do
  IO.puts 1
end

compile to

def route(%Plug.Conn{method: "GET", path_info: ["path"]}, _) do
  IO.puts 1
end
namespace :path0 do
  namespace :path1 do
    get :path2 do
      IO.puts 1
    end
  end
  
  post :path3 do
    IO.puts 2
  end
end

compile to

def route(%Plug.Conn{method: "GET", path_info: ["path0", "path1", "path2"]}, _) do
  IO.puts 1
end

def route((%Plug.Conn{method: "POST", path_info: ["path0", "path3"]}, _) do
  IO.puts 2
end
defmodule A do
  namespace :path1 do
    get :path2 do
      IO.puts 1
    end
  end
end

defmodule B do
  namespace :path0 do
    mount A
  end
end

compile to

defmodule B do
  def route(%Plug.Conn{method: "GET", path_info: ["path0", "path1", "paht2"]}, _) do
    IO.puts 1
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment