View Source Routex.Matchable (Phoenix Routes Extension Framework v0.3.0-alpha.4)

Matchables are an essential part of Routex. They are used to match run time routes with compile time routes.

This module provides functions to create Matchables, convert them to match pattern AST as well as function heads AST and to check if the routing values of two Matchable records match.

Summary

Functions

Returns whether two Matchable records match on their route defining properties. The first argument supports string interpolation syntax (e.g ":param" and "*") forming wildcards.

Converts a binary URL, Phoenix.Router.Route or (sigil) AST argument into a Matchable record.

Creates a function named name which the first argument matching a Matchable record pattern. Other arguments can be given with either a catch all or a pattern.

Returns a match pattern for given Matchable record or Phoenix.Router.Route. The pattern can be used either as function argument or in a function body. As the patterns bind values, they can be used to convert input from one pattern to another.

A non conflicting function mimicking to_string/1

Functions

Link to this function

match?(record_1, record_2)

View Source

Returns whether two Matchable records match on their route defining properties. The first argument supports string interpolation syntax (e.g ":param" and "*") forming wildcards.

Example

iex> route_record = %Phoenix.Router.Route{path: "/posts/:id"} |> Routex.Matchable.new()
   > matching_record = "/posts/1/foo=bar#top" |> Routex.Matchable.new()
   > unmatched_record = "/unmatched/1/foo=bar#op" |> Routex.Matchable.new()

iex> match?(route_record, matching_record)
true
iex match?(route_record, unmatched_record)
false
Link to this macro

matchable(args \\ [])

View Source (macro)
Link to this macro

matchable(record, args)

View Source (macro)

Converts a binary URL, Phoenix.Router.Route or (sigil) AST argument into a Matchable record.

Examples

iex> path = "/posts/1?foo=bar#top"
     > route = %Phoenix.Router.Route{path: "/posts/:id"}
     > ast = {:<<>>, [], ["/products/", {:"::", [], [{{:., [], [Kernel, :to_string]}, [from_interpolation: true], [{:id, [], Elixir}]}, {:binary, [], Elixir}]}]}

iex> path_match = Routex.Matchable.new(path)
{:matchable, [nil], ["posts", "1"], "foo=bar", "top", false}

iex> route_match = Routex.Matchable.new(route)
{:matchable, [], ["posts", ":id"], nil, nil, false}

iex> ast_match = Routex.Matchable.new(ast)
{:matchable, [], ["posts", {:"::", [], [{{:., [], [Kernel, :to_string]}, [from_interpolation: true], [{:id, [], Elixir}]}, {:binary, [], Elixir}]}], nil, nil, false}
Link to this function

placeholders_to_ast(path, dyns)

View Source
Link to this function

to_func(match_pattern, name, other_args \\ [], body)

View Source

Creates a function named name which the first argument matching a Matchable record pattern. Other arguments can be given with either a catch all or a pattern.

The Matchable pattern is bound to pattern

Example

iex> "/some/path"

  >  |> Matchable.new()
  >  |> Matchable.to_func(:my_func, [pattern_arg: "fixed", :catchall_arg], quote(do: :ok))

Returns a match pattern for given Matchable record or Phoenix.Router.Route. The pattern can be used either as function argument or in a function body. As the patterns bind values, they can be used to convert input from one pattern to another.

Example

iex> "/original/:arg1/:arg2" |> Routex.Matchable.new() |> Routex.Matchable.to_pattern()
{:{}, [], [:matchable, {:hosts, [], Routex.Matchable}, ["original", {:arg1, [], Routex.Matchable}, {:arg2, [], Routex.Matchable}], {:query, [], Routex.Matchable}, {:fragment, [], Routex.Matchable}, false]}

iex> "/recomposed/:arg2/:arg1" |> Routex.Matchable.new() |> Routex.Matchable.to_pattern()
{:{}, [], [:matchable, {:hosts, [], Routex.Matchable}, ["recomposed", {:arg2, [], Routex.Matchable}, {:arg1, [], Routex.Matchable}], {:query, [], Routex.Matchable}, {:fragment, [], Routex.Matchable}, false]}


iex> "/original/segment_1/segment_2" |> Routex.Matchable.new() |> Routex.Matchable.to_pattern()
{:{}, [], [:matchable, {:hosts, [], Routex.Matchable}, ["original", "segment_1", "segment_2"], {:query, [], Routex.Matchable}, {:fragment, [], Routex.Matchable}, false]}

A non conflicting function mimicking to_string/1