Multilingual.View (multilingual v0.1.11)

Summary

Functions

Fetches a key from the private View data in the connection and raises an error is not view is found.

Fetches a key from the private View data in the connection or socket. Returns nil if no view data is found.

Functions

Link to this function

fetch_key(conn, key)

Fetches a key from the private View data in the connection and raises an error is not view is found.

Examples

iex> view = %Multilingual.View{locale: "en", path: "/about"}
...> conn = Plug.Conn.put_private(%Plug.Conn{}, :multilingual, view)
...> Multilingual.View.fetch_key(conn, :path)
"/about"

iex> view = %Multilingual.View{locale: "en", path: "/about"}
...> conn = Plug.Conn.put_private(%Plug.Conn{}, :multilingual, view)
...> Multilingual.View.fetch_key(conn, :bad_key)
** (FunctionClauseError) no function clause matching in Multilingual.View.fetch_key/2

iex> assert_raise Multilingual.MissingViewDataInConnError, fn ->
...>  Multilingual.View.fetch_key(%Plug.Conn{}, :locale)
...> end

iex> view = %Multilingual.View{locale: "en", path: "/about"}
...> socket = Phoenix.LiveView.put_private(%Phoenix.LiveView.Socket{}, :multilingual, view)
...> Multilingual.View.fetch_key(socket, :path)
"/about"

iex> view = %Multilingual.View{locale: "en", path: "/about"}
...> socket = Phoenix.LiveView.put_private(%Phoenix.LiveView.Socket{}, :multilingual, view)
...> Multilingual.View.fetch_key(socket, :bad_key)
** (FunctionClauseError) no function clause matching in Multilingual.View.fetch_key/2

iex> assert_raise Multilingual.MissingViewDataInSocketError, fn ->
...>  Multilingual.View.fetch_key(%Phoenix.LiveView.Socket{}, :locale)
...> end
Link to this function

get_key(conn, key)

Fetches a key from the private View data in the connection or socket. Returns nil if no view data is found.

Raises an error if an erroneous key is requested.

Examples

iex> view = %Multilingual.View{locale: "en", path: "/about"}
...> conn = Plug.Conn.put_private(%Plug.Conn{}, :multilingual, view)
...> Multilingual.View.get_key(conn, :path)
"/about"

iex> view = %Multilingual.View{locale: "en", path: "/about"}
...> conn = Plug.Conn.put_private(%Plug.Conn{}, :multilingual, view)
...> Multilingual.View.get_key(conn, :bad_key)
** (FunctionClauseError) no function clause matching in Multilingual.View.get_key/2

iex> Multilingual.View.get_key(%Plug.Conn{}, :path)
nil

iex> view = %Multilingual.View{locale: "en", path: "/about"}
...> socket = Phoenix.LiveView.put_private(%Phoenix.LiveView.Socket{}, :multilingual, view)
...> Multilingual.View.get_key(socket, :path)
"/about"

iex> view = %Multilingual.View{locale: "en", path: "/about"}
...> socket = Phoenix.LiveView.put_private(%Phoenix.LiveView.Socket{}, :multilingual, view)
...> Multilingual.View.get_key(socket, :bad_key)
** (FunctionClauseError) no function clause matching in Multilingual.View.get_key/2

iex> Multilingual.View.get_key(%Phoenix.LiveView.Socket{}, :locale)
nil