Skip to content

Instantly share code, notes, and snippets.

@kerryb
kerryb / changeset-form-issue.livemd
Last active December 13, 2023 14:38
LiveBook demonstrating an Ecto changeset/Phoenix form issue
View changeset-form-issue.livemd

Changeset/Form issue

Mix.install([
  {:phoenix_live_view, "~> 0.20.1"},
  {:phoenix_ecto, "~> 4.4"}
])

Create a struct

@kerryb
kerryb / stack.rb
Created October 9, 2023 13:04
A simple stack, TDDed without using a testing framework. Unfortunately the individual steps weren’t recorded.
View stack.rb
class Stack
class Empty < RuntimeError; end
def initialize
@items = []
end
def empty?
@items.count == 0
@kerryb
kerryb / parser.ex
Created October 12, 2022 14:46
Horrible code to convert some nested text data to json
View parser.ex
text
|> String.split("\n")
|> Enum.reduce({["{"], " "}, fn line, {output, current_indent} ->
case Regex.run(~r/^( +)(.*): ?(.*)/, line, capture: :all_but_first) do
[indent, key, ""] ->
close_braces =
if indent < current_indent do
Enum.map_join(
(String.length(current_indent) - 4)..String.length(indent)//-4,
"",
@kerryb
kerryb / space.exs
Created October 7, 2022 18:27
Who’s in space?
View space.exs
#!/usr/bin/env elixir
Mix.install([
{:httpoison, "~> 1.8"},
{:jason, "~> 1.3"}
])
defmodule Space do
def run do
fetch_data()
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 19:00
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 July 2, 2018 10:53
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 February 23, 2017 21:45
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 January 12, 2016 14:18
Blockchain verification
View blockchain.txt
Verifying that +kerryb is my blockchain ID. No, me neither. https://onename.com/kerryb