ExGram.Router.Compiler (ex_gram_router v0.1.0)

Copy Markdown View Source

@before_compile hook that generates the handle/2 function from the routing tree built by the DSL macros.

This module is used internally by ExGram.Router and should not be referenced directly.

What gets generated

Given this router definition:

use ExGram.Router

scope do
  filter :command, :start
  handle &MyBot.start/1
end

scope do
  handle &MyBot.fallback/1
end

The compiler generates:

def handle(update_info, context) do
  ExGram.Router.Dispatcher.dispatch(update_info, context, __exgram_routing_tree__())
end

def __exgram_routing_tree__ do
  [
    %ExGram.Router.Scope{
      filters: [{ExGram.Router.Filters.Command, :start}],
      children: [],
      handler: {MyBot, :start, 1}
    },
    %ExGram.Router.Scope{
      filters: [],
      children: [],
      handler: {MyBot, :fallback, 1}
    }
  ]
end