Created
September 7, 2015 07:50
-
-
Save amencarini/dd50a97a6f2685396843 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This works | |
defmodule Resources do | |
defmacro __using__(resource) do | |
quote bind_quoted: [resource: resource] do | |
def one(resource, id), do: IO.puts "/api/#{resource}/#{id}" | |
end | |
end | |
end | |
defmodule MainApp do | |
use Resources, :products | |
def test, do: one(:products, 12) | |
end | |
MainApp.test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This gives me: | |
# ** (CompileError) iex:4: function plural/0 undefined | |
defmodule Resources do | |
defmacro __using__({singular, plural}) do | |
quote bind_quoted: [singular: singular, plural: plural] do | |
def one(singular, id), do: IO.puts "/api/#{plural}/#{id}" | |
end | |
end | |
end | |
defmodule MainApp do | |
use Resources, {:product, :products} | |
def test, do: one(:products, 12) | |
end | |
MainApp.test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This also works | |
defmodule Resources do | |
defmacro __using__({singular, plural}) do | |
quote do | |
def one(unquote(singular), id), do: IO.puts "/api/#{unquote(plural)}/#{id}" | |
end | |
end | |
end | |
defmodule MainApp do | |
use Resources, {:product, :products} | |
def test, do: one(:product, 12) | |
end | |
MainApp.test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about
?