Skip to content

Instantly share code, notes, and snippets.

View PragTob's full-sized avatar

Tobias Pfeiffer PragTob

View GitHub Profile
@PragTob
PragTob / rails.ex
Created December 24, 2017 10:36
Railsway oriented programming in elixir
def create(order_params, customer, auth) do
with {:ok, order} <- validate_order(order_params, customer),
{:ok, order} <- validate_tour_shipment_in_backend(order, auth),
{:ok, order} <- Repo.insert(order) do
{:ok, order}
else
{:error, changeset} -> {:error, changeset}
end
end
@PragTob
PragTob / doctest.ex
Last active July 26, 2017 11:00
doctests
@doc """
## Examples
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
iex> Agent.get_and_update(pid, fn(state) -> {state, state + 1} end)
42
iex> Agent.get(pid, fn(state) -> state end)
43
"""
@PragTob
PragTob / type_specs.ex
Created July 24, 2017 20:47
type specs
@type job_name :: String.t | atom
@spec benchmark(Suite.t, job_name, fun, module) :: Suite.t
def benchmark(suite = %Suite{scenarios: scenarios}, job_name, function, printer \\ Printer) do
#...
end
@PragTob
PragTob / changeset.ex
Last active July 24, 2017 20:44
small ecto changeset example
def new_changeset(model, params \\ %{}) do
model
|> cast(params, ~w(name username))
|> validate_required(~w(name username))
|> unique_constraint(:username)
|> validate_length(:username, min: 1, max: 20)
end
def registration_changeset(model, params) do
model
@PragTob
PragTob / ecto_preloading.ex
Created July 24, 2017 20:03
Ecto explicit preloading in action
iex(13)> user = Repo.get_by(User, name: "Homer")
iex(14)> user.videos
#Ecto.Association.NotLoaded<association :videos is not loaded>
iex(17)> user = Repo.preload(user, :videos)
iex(18)> user.videos
# all the videos
@PragTob
PragTob / phoenix_controller_explicit.ex
Created July 24, 2017 19:51
Phoenix Controller Action is explicit
def new(conn, _params) do
changeset = User.new_changeset(%User{})
render conn, "new.html", changeset: changeset
end
@PragTob
PragTob / railway.ex
Created July 24, 2017 19:43
railway oriented programming
with {:ok, record} <- validate_data(params),
{:ok, record} <- validate_in_other_system(record),
{:ok, record} <- Repo.insert(record) do
{:ok, record}
else
{:error, changeset} -> {:error, changeset}
end
@PragTob
PragTob / pipe.exs
Created July 24, 2017 19:19
Nice pipe
config
|> Benchee.init
|> Benchee.system
|> Benchee.benchmark("job", fn -> magic end)
|> Benchee.measure
|> Benchee.statistics
|> Benchee.Formatters.Console.output
|> Benchee.Formatters.HTML.output
mp3_byte_size = (byte_size(binary) - 128)
<< _ :: binary-size(mp3_byte_size), id3_tag :: binary >> = binary
<< "TAG",
title :: binary-size(30),
artist :: binary-size(30),
album :: binary-size(30),
year :: binary-size(4),
comment :: binary-size(30),
_rest :: binary >> = id3_tag
@PragTob
PragTob / matching.exs
Last active July 28, 2017 08:14
Elixir Pattern matching
defmodule Patterns do
def greet(%{name: name, age: age}) do
IO.puts "Hi there #{name}, what's up at #{age}?"
end
def greet(%{name: "José Valim"}) do
IO.puts "Hi José, thanks for elixir! <3"
end
def greet(%{name: name}) do
IO.puts "Hi there #{name}"
end