Skip to content

Instantly share code, notes, and snippets.

View Repo.ex
defmodule MyApp.Repo do
require Logger
alias Ecto.Association.NotLoaded
alias Ecto.{Changeset, Schema}
alias MyApp.Auth.User
...
@doc """
Wrapper round `Ecto.Repo.insert/2`, which logs the fields of the inserted
@kerryb
kerryb / README.md
Last active May 19, 2019
Basic given/when/then macros for ExUnit
View README.md

README

A spike looking at adding basic given-when-then steps to ExUnit tests.

Features

  • define tests as sequences of calls to given_, when_ and then_ (unfortunately when is a reserved word)
  • match steps by calling defwhen etc with a string matching the one used in the step
  • interpolate values into step descriptions using {braces}
    • placeholder variable names are available as methods on the magic args variable
@kerryb
kerryb / child.ex
Last active Jul 2, 2018
Supervised task, with timeout
View child.ex
defmodule Child do
def task(parent) do
Process.sleep(:timer.seconds(2))
send(parent, :finished)
end
end
@kerryb
kerryb / ytd.bash
Last active Feb 23, 2017
Year-to-date running stats
View ytd.bash
ytd() {
ytd=$(find ~/Dropbox/Apps/tapiriik -name $(date +%Y)-*_Running*.tcx -print0 | xargs -0 -n1 awk '/DistanceMeters/ {a=$0} END {gsub(/\s*<[^>]*>/, "", a); print a}' | paste -sd+ - | sed 's/\(.*\)/0.000621371 * (\1)/' | bc | sed 's/\(\..\).*/\1/')
proj=$(echo "$ytd * 365 / $(date +%j)" | bc)
echo "YTD: $ytd. Projected: $proj"
}
View roman_numerals.rb
class RomanNumerals
NUMBERS = {
1000 => "M",
900 => "CM",
500 => "D",
400 => "CD",
100 => "C",
90 => "XC",
50 => "L",
40 => "XL",
@kerryb
kerryb / blockchain.txt
Created Jan 12, 2016
Blockchain verification
View blockchain.txt
Verifying that +kerryb is my blockchain ID. No, me neither. https://onename.com/kerryb
View keybase.md

Keybase proof

I hereby claim:

  • I am kerryb on github.
  • I am kerryb (https://keybase.io/kerryb) on keybase.
  • I have a public key whose fingerprint is 8DA4 4B51 5A90 D3AE 07B9 2FD2 5E07 C65F E855 9D1D

To claim this, I am signing this object:

@kerryb
kerryb / application.rb
Created Jun 24, 2014
Simple rails example of form interacting with non-activerecord backend code
View application.rb
# config/application.rb
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_model/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
@kerryb
kerryb / .gitconfig
Last active Aug 29, 2015
The non-personal bits of my git config
View .gitconfig
[color]
diff = auto
status = auto
branch = auto
[instaweb]
browser = open
[merge]
keepBackup = false
tool = diffmerge
defaultToUpstream = true
@kerryb
kerryb / blocks.rb
Created Dec 10, 2013
Ruby blocks, procs, map and reduce
View blocks.rb
# A proc is just a block of code that can be called later:
say_hello = -> { 2 + 2 }
say_hello.call # => 4
# Procs can take parameters:
double = ->(x) { x * 2 }