Skip to content

Instantly share code, notes, and snippets.

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 / space.exs
Created October 7, 2022 18:27
Who’s in space?
#!/usr/bin/env elixir
Mix.install([
{:httpoison, "~> 1.8"},
{:jason, "~> 1.3"}
])
defmodule Space do
def run do
fetch_data()
@kerryb
kerryb / parser.ex
Created October 12, 2022 14:46
Horrible code to convert some nested text data to json
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 / stack.rb
Created October 9, 2023 13:04
A simple stack, TDDed without using a testing framework. Unfortunately the individual steps weren’t recorded.
class Stack
class Empty < RuntimeError; end
def initialize
@items = []
end
def empty?
@items.count == 0
@kerryb
kerryb / changeset-form-issue.livemd
Last active December 13, 2023 14:38
LiveBook demonstrating an Ecto changeset/Phoenix form issue

Changeset/Form issue

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

Create a struct