Skip to content

Instantly share code, notes, and snippets.

View hl's full-sized avatar

Henricus Louwhoff hl

View GitHub Profile
@hl
hl / context.ex
Last active August 28, 2021 04:55
defmodule Context do
@moduledoc false
defmacro __using__(opts) do
repo = Keyword.fetch!(opts, :repo)
quote do
import Context, only: [context: 1, context: 2]
Module.put_attribute(__MODULE__, :__repo__, unquote(repo))
@hl
hl / repo.ex
Last active April 30, 2024 05:33
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
require Ecto.Query
@spec fetch_one(Ecto.Queryable.t(), Keyword.t()) ::
{:ok, struct()} | {:error, :not_found}
def fetch_one(queryable, opts \\ []) do
defmodule Normalize do
import Ecto.Changeset
@spec normalize(map(), Keyword.t()) :: {:ok, map()} | {:error, Ecto.Changeset.t()}
def normalize(params, input_schema) do
types =
Map.new(input_schema, fn {field, opts} ->
{field, List.first(opts)}
end)
@hl
hl / james.ex
Created December 6, 2023 12:55
defmodule James do
@moduledoc """
Agent: James Bond
"""
defmodule State do
@moduledoc """
Data module to defined and modify state.
"""
defstruct [:id, :name]
@hl
hl / blog.janet
Created January 15, 2024 10:51
Blog with Janet
(use joy)
(import markable)
(import filesystem)
# Layout
(defn app-layout [{:body body :request request}]
(text/html
(doctype :html5)
[:html {:lang "en"}
[:head
@hl
hl / storage.ex
Created January 22, 2024 12:27
simple persistent_term storage
defmodule Storage do
@moduledoc """
This module provides a wrapper around the :persistent_term module
https://www.erlang.org/doc/man/persistent_term
"""
@type storage :: atom()
@type key :: atom()
@type value :: any()
defmodule Engine do
@moduledoc """
Engine that can run a series of stages.
It will return a tuple containing the last effect and all the effects.
## Example
defmodule HelloWorld do
import Engine