3
在鳳凰城創建的應用程序的方法陣列的方法(PARAM)的綁定變量的一部分,我想實現這樣的如何創建由宏
#= Bouncers.Bouncer.check_entry(conn, %MyApp.User{})
#=> true
defmodule Bouncers.Bouncer do
alias Bouncers.Areas
def check_entry(conn, resource) do
verify(resource, conn.path_info)
end
defp verify(resource, path) do
try do
Areas.verify(resource, path)
rescue
_ -> false
end
end
end
defmodule Bouncers.Doors do
defmacro bouncer_for(model, allowed_routes) do
for route <- allowed_routes do
quote bind_quoted: [route: route, resource: model] do
def verify(resource, route), do: true
end
end
end
end
defmodule Bouncers.Areas do
require Bouncers.Doors
import Bouncers.Doors
bouncer_for %MyApp.User{}, [
["api", "users"]
]
end
bouncer_for
授權創建多個verify
方法一個結構和path_info
陣列
bouncer_for %MyApp.User{}, [
["api", "users"],
["api", "posts"]
]
將創建
def verify(%MyApp.User{}, ["api", "users"], do: true
def verify(%MyApp.User{}, ["api", "posts"], do: true
它適用於這些paths
,但我堅持格式爲["api", "users", _ ]
的路徑,這會引發有關未綁定變量的錯誤。我想這樣的事情ATLEAST,
bouncer_for %MyApp.User{}, [
["api", "users", "*"],
]
而且隨着_
取代"*"
由宏產生的方法的參數內,。任何指針?
邊注:如果您已經'import'一個模塊,我認爲額外的'require'不必要。 –