Skip to content

Instantly share code, notes, and snippets.

View christhekeele's full-sized avatar
💜

Christopher Keele christhekeele

💜
View GitHub Profile
@christhekeele
christhekeele / default_behaviour.ex
Last active March 4, 2025 17:41
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 / mnemonix_repos.md
Last active July 2, 2017 12:52
How Mnemonix's repository pattern is implemented

Synopsis

Mnemonix is a key/value adapter library that employs a repository pattern. I wanted to support a few things with it:

  • Multiple 'feature sets'--collections of functions that may or may not be implemented for an adapter
  • A single unified API inside the core module incorporating all feature sets
  • Compile-time and runtime support for configuring repositories
  • Application-controlled and DIY repo supervision
  • The ability for library users to build custom modules with only particular feature sets
@christhekeele
christhekeele / ecto_adapter_custom.ex
Last active August 31, 2018 08:42
Scaffold for a full implementation of the Ecto Adapter behaviours. (Ecto v2.1.4)
defmodule Ecto.Adapter.Custom do
####
# Ecto.Adapter
##
# TYPES
# @type t :: Ecto.Adapter.t
@type t :: Ecto.Adapter.Custom
@christhekeele
christhekeele / guard.ex
Last active September 17, 2016 04:38
Check quoted expressions for validity inside guards.
defmodule Guard do
@guardables ~W[
== != === !== > >= < <=
and or not
+ - * /
<>
in
is_atom is_binary is_bitstring is_boolean
is_float is_function is_integer is_list
@christhekeele
christhekeele / mix.exs
Last active May 21, 2016 19:09
Local Git Repos for Mix
defmodule MyApp.Mixfile do
use Mix.Project
def project do
[app: :my_app,
version: "0.0.1",
elixir: "~> 1.2",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
@christhekeele
christhekeele / 1-indirect_uses_tracker.ex
Last active November 29, 2024 21:29
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 / 1-uses_tracker.ex
Last active November 29, 2024 21:29
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 / pipeline.rb
Last active January 26, 2016 19:01
Cool use of subclassing Module in Ruby.
class Pipeline < Module
attr_accessor :transforms
def initialize(*transforms, &implementation)
@transforms = transforms
instance_eval &implementation if block_given?
end
def call(*args, &block)
transformers.reduce(block || default_block) do |pipeline, transformer|

Keybase proof

I hereby claim:

  • I am christhekeele on github.
  • I am christhekeele (https://keybase.io/christhekeele) on keybase.
  • I have a public key whose fingerprint is FD54 8218 8153 75DB 2F2E 31E4 4F18 C524 2FAE 4D97

To claim this, I am signing this object:

# FORCE RAILS REDIRECTS TO HONOR CONTENT-TYPE HEADERS
# https://github.com/rails/rails/issues/17194
require 'action_controller/metal/redirecting'
module ActionController
module Redirecting
def redirect_to(options = {}, response_status = {}) #:doc:
raise ActionControllerError.new("Cannot redirect to nil!") unless options