View Source Routex.Branching (Phoenix Routes Extension Framework v0.3.0-alpha.3)

Provides a set of functions to build branched variants of macro's

Summary

Functions

Link to this function

branch_macro(patterns, match_binding, clause_transformer, argument_transformer, module, fun, opts \\ [])

View Source

Takes a list of match patterns and creates the AST for branching variants for all arities of macro in module.

** Args

  • match_binding: the argument for the case clause
  • patterns: the match patterns of the case clause
  • transformer: transforms a original arguments value

** Example

We want to create a branching variant of the url macro in Phoenix,.VerifiedRoutes module. The original macro generates code that simply prints the given path argument, but we want to it to write multiple clauses and prefix the given argument based on the clause.

defmacro url(path, opts \ []) do -> quote do IO.puts(path) end

Given this code:

patterns = ["en", "nl"] match_binding = var!(external_var) arg_transformer = fn pattern, arg -> "europe/" <> pattern <> "/" <> arg end

branch_macro(patterns, match_binding, arg_transformer, OriginalModule, :url,

as: :url,
orig: :url_original,
arg_pos: fn arity -> arity - 1 end)

A new macro is build which outputs the AST of the original macro, wrapped in a case clause given transformed arguments.

defmacro url(path, opts \ []) do

 quote do
   case match_binding do
         "en" -> Original.Module.url("europe/en/" <> url, opts)
         "nl" -> Original.Module.url("europe/nl/" <> url, opts)
     end

end end

Link to this function

build_case(patterns, match_binding, clause_transformer, argument_transformer, module, fun, args, opts)

View Source
Link to this function

build_default(module, fun, args)

View Source