Skip to content

Instantly share code, notes, and snippets.

@brainlid
brainlid / .iex.exs
Created April 2, 2024 12:52
My .iex.exs file that lives in my Home directory. Gets loaded and used in every IEx session on my machines. I borrowed and stole all the great ideas from others an tweaked them over the years.
Application.put_env(:elixir, :ansi_enabled, true)
# IEx shell customization for color
# - Added as a comment: https://thinkingelixir.com/course/code-flow/module-1/pipe-operator/
# - Original source: https://samuelmullen.com/articles/customizing_elixirs_iex/
# - Can add to a specific project or can put in the User home folder as a global config.
# Updates from:
# - https://github.com/am-kantox/finitomata/blob/48e1b41b21f878e9720e6562ca34f1ce7238d810/examples/ecto_intergation/.iex.exs
timestamp = fn ->
{_date, {hour, minute, _second}} = :calendar.local_time
@brainlid
brainlid / index.ex
Last active February 14, 2024 21:22
Example files for a LiveView blog post that starts an async process using Phoenix Async Assigns to perform work and send message back to the LiveView. https://fly.io/phoenix-files/abusing-liveview-new-async-assigns-feature/
defmodule MyAppyWeb.TaskTestLive.Index do
use MyAppyWeb, :live_view
require Logger
alias Phoenix.LiveView.AsyncResult
@impl true
def mount(_params, _session, socket) do
socket =
socket
|> assign(:async_result, %AsyncResult{})
@brainlid
brainlid / index.ex
Last active November 14, 2023 17:23
Example files for a LiveView blog post that starts an async Task to perform work and send message back to the LiveView. https://fly.io/phoenix-files/star-cross-live-view-processes/
defmodule MyAppyWeb.TaskTestLive.Index do
use MyAppWeb, :live_view
require Logger
@impl true
def mount(_params, _session, socket) do
# Trap exits to catch when a Task is forcibly cancelled.
Process.flag(:trap_exit, true)
socket =
defmodule MyApp.Books.Book do
use Ecto.Schema
import Ecto.Query, warn: false
import Ecto.Changeset
import MyApp.ChangesetHelpers
schema "books" do
field :name, :string
field :genres, {:array, :string}, default: []
defmodule Acme.Repo do
use Ecto.Repo,
otp_app: :acme,
adapter: Ecto.Adapters.Postgres
def with_prefix(prefix) do
module_atom = Module.concat([Acme, Repo, WithPrefix, Macro.camelize(prefix)])
# We could not find a better way to see if this module already existed
if !Kernel.function_exported?(module_atom, :prefix, 0) do
@brainlid
brainlid / row_poly.ex
Created February 27, 2020 23:25
Row Polymorphism example in Elixir
defmodule Point2D do
defstruct [x: 0, y: 0]
end
defmodule Point3D do
defstruct [x: 0, y: 0, z: 0]
end
defmodule RowPolymorphism do
def get_x_y(%{x: x, y: y} = _data) do
@brainlid
brainlid / Readme.md
Created May 15, 2019 19:27 — forked from philipz/Readme.md
GitLab Runner on a Kubernetes cluster
  1. Create namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: gitlab
  1. kubectl create -f ./namespace.yaml
@brainlid
brainlid / Dockerfile
Created July 3, 2018 18:36
Describe solution for user ownership of files
FROM elixir:1.6.5-alpine
ARG DEV_USER=1000
RUN apk add --no-cache \
# bash terminal Support
bash \
# development - could be separated/virtual for removal, but lets just get the bin going -BJG
inotify-tools wget curl smem tar gcc g++ libc-dev libffi-dev make \
# utilities
@brainlid
brainlid / config.ex
Created March 26, 2018 15:37 — forked from bitwalker/config.ex
Useful config wrapper for Elixir
defmodule Config do
@moduledoc """
This module handles fetching values from the config with some additional niceties
"""
@doc """
Fetches a value from the config, or from the environment if {:system, "VAR"}
is provided.
An optional default value can be provided if desired.
@brainlid
brainlid / index.html
Created August 4, 2016 13:22 — forked from anonymous/index.html
JS Bin [Queue for rendering 1 at a time] // source http://jsbin.com/basuzi
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="[Queue for rendering 1 at a time]">
<script src="https://cdnjs.cloudflare.com/ajax/libs/q.js/1.0.1/q.js"></script>
<script src="https://npmcdn.com/@reactivex/rxjs@5.0.0-beta.6/dist/global/Rx.umd.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>