Skip to content

Instantly share code, notes, and snippets.

View christhekeele's full-sized avatar
💜

Christopher Keele christhekeele

💜
View GitHub Profile
@christhekeele
christhekeele / 1-indirect_uses_tracker.ex
Last active February 10, 2024 02:44
Elixir metaprogramming module usage: A way to track when certain modules are used, and an example adapter/plugin architecture built on top.
# For simpler use cases, see the UsesTracker instead:
# https://gist.github.com/christhekeele/e858881d0ca2053295c6e10d8692e6ea
###
# A way to know, at runtime, what modules a module has used at compile time.
# In this case, you include `IndirectUsesTracker` into a module. When that module gets
# used in some other module, it makes that module registerable under a namespace of your choosing.
# When the registerable module is used into a third module, that third module will know at runtime which
# registerables were `use`d in it at compile time, via a function titled after the namespace.
@christhekeele
christhekeele / elixir-bio.md
Last active October 26, 2023 03:42
My activity in Elixir since 2013!

In roughly chronological order:

@christhekeele
christhekeele / postgres-uuid-reflection.md
Last active July 20, 2023 16:29
Automatic UUID Reflection in Postgres

Automatic UUID Reflection in Postgres

You have a system backed by a Postgres database that uses UUIDs as primary keys for everything. This is nice:

  • You can generate ids on clients before transmitting writes to servers if you wish.
  • You don't have to worry about exhausting the integer size allocated to your auto-incrementing primary keys.[^1]
  • You don't have to worry about enumeration attacks from auto-incrementing primary keys.
  • You can identify and index on data that has no natural key, without the challenges of auto-incrementing primary keys.

The Problem

@christhekeele
christhekeele / default_behaviour.ex
Last active July 4, 2023 04:22
Behaviours with Defaults for Elixir
defmodule Default.Behaviour do
@moduledoc """
Creates a behaviour that carries its own default implementation.
When used into a behaviour module, when that module in turn is used, all functions
defined on it are given to the using module.
This allows you to have concrete implementations of the behaviour's default functionality
for testing, unlike cramming them all into a __using__ macro.
@christhekeele
christhekeele / 1-uses_tracker.ex
Last active May 27, 2023 00:42
Metaprogramming Elixir module usage: A simple way to know what modules a module has used in Elixir.
# For more elaborate use cases, see the IndirectUsesTracker instead:
# https://gist.github.com/christhekeele/fc4e058ee7d117016b9b041b83c6546a
###
# A way to know, at runtime, what modules a module has used at compile time.
# In this case, you include `UsesTracker` into a module. When that module gets
# used in some other module, it registers itself with the other module.
##
defmodule UsesTracker do
@christhekeele
christhekeele / ALLOWABLE.md
Last active May 16, 2023 10:27
Allowable: A Ruby gem DSL for compound conditionals.

Allowable

A micro-gem DSL for compound conditionals.

Allowable lets you decompose large/long conditional chains into readable, testable, and inspectable segments with Ruby blocks.

Installation

@christhekeele
christhekeele / guard_helpers.ex
Last active September 1, 2020 20:48
A defguard macro written for Elixir v0.11.something a while back. I don't remember anything breaking at the time. Written for a library that was supposed to help AST transformations, in part by creating guards for particular AST constructs.
defmodule Guard.Helpers do
@moduledoc """
Tools for creating custom guards. Deprecated in favor of `Kernel.defguard`.
"""
@doc """
Creates a macro that's aware of its presence in a guard.
Taken from https://github.com/elixir-lang/elixir/blob/df8b216357e023e4ef078be396fed6b873d6a938/lib/elixir/lib/kernel.ex#L1601-L1615,
@christhekeele
christhekeele / compile.markdown.ex
Last active August 16, 2020 08:51
Turn markdown files into HTML in Elixir projects at compile-time.
####
# USAGE:
#
# 1. Add Earmark to your dependencies
# 2. Place this file in lib/mix/compile.markdown.ex
# 3. Add to your mix.exs project's compilers:, ie:
# compilers: [:markdown] ++ Mix.compilers()
# 4. Profit?
defmodule Mix.Tasks.Compile.Markdown do
@christhekeele
christhekeele / foo.ex
Created June 12, 2020 07:56 — forked from 0x6a68/foo.ex
Spec to Callback
defmodule MyApp.Foo do
@on_definition MyApp.SpecToCallback
@spec bar(String.t()) :: String.t()
def bar(foobar) do
impl().bar(foobar)
end
defp impl, do: Application.get_env(:my_app, :my_app_foo_impl, __MODULE__.DefaultImpl)
end
@christhekeele
christhekeele / README.md
Last active March 17, 2020 19:08 — forked from twolfson/README.md
Leverage Flask-SQLAlchemy with Celery

Last update: 2020-03-17

Flask-SQLAlchemy has some nice built-ins (e.g. accessing query directly on classes). To continue leveraging these nicities while still inside of a Celery worker, we need to make sure we setup/teardown in a similar fashion to Flask-SQLAlchemy does on Flask.

Setup

Flask-SQLAlchemy uses create_scoped_session at startup which avoids any setup on a per-request basis.

https://github.com/mitsuhiko/flask-sqlalchemy/blob/2.0/flask_sqlalchemy/__init__.py#L668

This means Celery can piggyback off of this initialization.