Skip to content

Instantly share code, notes, and snippets.

// akka-actor cheatsheet
// extract from http://doc.akka.io/actors-scala
class MyActor extends Actor { // Logging is included in Actor
// messages
def receive = {
case "foo" =>
doBar
case "logging" =>
@doc """
Validate each array element with validator
Examples
iex> changeset
...> |> validate_each(:emails, &validate_email)
...> |> validate_each(:phones, &validate_format(&1, &2, ~r/\d+/))
...> |> validate_each(:phones, &validate_length/3, is: 4)
"""
def validate_each(changeset, field, validator, opts) do
@teamon
teamon / prof.rb
Created June 5, 2013 14:56
Profile rails controller with ruby-prof
# gem "ruby-prof"
around_filter :profiler
def profiler(&block)
data = RubyProf::Profile.profile(&block)
filepath = Rails.root.join("tmp", "profiler.html")
File.open(filepath, "w") do |f|
RubyProf::CallStackPrinter.new(data).print(f)
@teamon
teamon / gist:1008461
Created June 4, 2011 22:58
OpenOCD setup for Mac OSX
1. Download and install d2xx drivers http://www.ftdichip.com/Drivers/D2XX/MacOSX/D2XX1.0.4.dmg (from http://www.ftdichip.com/Drivers/D2XX.htm)
2. Install OpenOCD from homebrew (http://mxcl.github.com/homebrew/)
$ brew install openocd --enable-ft2232_libftdi
@teamon
teamon / gist:1839423
Created February 15, 2012 22:18 — forked from wesQ3/gist:1782417
POST /api/v1/projects.json
// POST http://staginginfo.marketmaker4.com/api/v1/projects.json
{
"project": {
"actor_uid": "7ajxdiGvTvZ8DJoT2uY4",
"list": {
"name": "Test 1329343404",
"companies": [
{
"contact_person": {
"email": "dellis@monachem.mc",
@teamon
teamon / avatar.ex
Created September 9, 2016 21:30
Quickly create avatar image from user name (initials)
# Much simplified version based on https://github.com/zhangsoledad/alchemic_avatar
# Avatar.generate/2 returns a Plug.Upload that can be passed directly
# into Ecto.Changeset (as for example fallback image)
defmodule Avatar do
@defaults [
size: 300,
font_path: Application.app_dir(:my_app, "priv/fonts/Roboto.ttf"),
font_size: 40,
font_weight: 500,
defmodule Twin do
@moduledoc """
See http://teamon.eu/2017/different-approach-to-elixir-mocks-doubles/
"""
## PROXY
defmodule Proxy do
def unquote(:"$handle_undefined_function")(fun, args) do
[{__MODULE__, mod} | rest] = Enum.reverse(args)
defmodule Test do
def run(x) do
with :error <- try_one(x),
:error <- try_two(x),
:error <- try_three(x) do
:error
end
end
defp try_one(1), do: {:ok, "one"}
@teamon
teamon / mix_xref.txt
Created October 19, 2017 08:49
tesla ref
⌘ ~/code/tesla (master) λ mix xref graph
lib/tesla.ex
├── lib/tesla/adapter/hackney.ex (compile)
│ ├── lib/tesla/multipart.ex (compile)
│ └── lib/tesla.ex
├── lib/tesla/adapter/httpc.ex (compile)
│ ├── lib/tesla/adapter/shared.ex (compile)
│ ├── lib/tesla/multipart.ex (compile)
│ └── lib/tesla.ex
├── lib/tesla/adapter/ibrowse.ex (compile)
defmodule Sync.Middleware.RateLimit do
@defaults [
delay: 2000,
max_retries: 5
]
def call(env, next, opts) do
opts = opts || []
delay = Keyword.get(opts, :delay, @defaults[:delay])
max_retries = Keyword.get(opts, :max_retries, @defaults[:max_retries])