Last active
December 22, 2016 01:27
-
-
Save kana-sama/ee1c7b7c0bb9f702c5db479d737fd34a to your computer and use it in GitHub Desktop.
metaprogramming in elixir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule BaseApp do | |
def new_conn do | |
%{} | |
|> put_status(:ok) | |
end | |
def render(%{status: status} = conn) do | |
IO.puts "Status: #{status}" | |
conn | |
end | |
def put_status(conn, status) when is_atom(status) do | |
status_code = case status do | |
:ok -> | |
200 | |
:bad_request -> | |
400 | |
:not_found -> | |
404 | |
_ -> | |
exit("Invalid status atom") | |
end | |
put_status(conn, status_code) | |
end | |
def put_status(conn, status) when is_number(status) do | |
put_in(conn[:status], status) | |
end | |
end | |
defmodule MyApp do | |
import BaseApp | |
def index(conn) do | |
conn | |
|> put_status(:not_found) | |
end | |
def run do | |
new_conn | |
|> index | |
|> render | |
end | |
end | |
MyApp.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment