Skip to content

Instantly share code, notes, and snippets.

View gvaughn's full-sized avatar
:atom:
Never trust an atom. They make up everything.

Greg Vaughn gvaughn

:atom:
Never trust an atom. They make up everything.
View GitHub Profile
@gvaughn
gvaughn / repo.ex
Created April 29, 2024 15:12 — forked from hl/repo.ex
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
@gvaughn
gvaughn / component_under_test.ex
Created January 26, 2024 17:12 — forked from mcrumm/component_under_test.ex
Testing Phoenix.LiveComponent in Isolation
# lib/party_web/components/example_component.ex
defmodule PartyWeb.ExampleComponent do
@moduledoc """
An example LiveComponent under test.
"""
use Phoenix.LiveComponent
def render(assigns) do
~H"""
<div>
@gvaughn
gvaughn / fizzbuzz.ex
Created December 31, 2022 18:13 — forked from toranb/fizzbuzz.ex
fizzbuzz with Axon (collaboration with Ian Warshak)
defmodule Mlearning do
@moduledoc false
def mods(x) do
[rem(x, 3), rem(x, 5), rem(x, 15)]
end
def fizzbuzz(n) do
cond do
rem(n, 15) == 0 -> [0, 0, 1, 0]
@gvaughn
gvaughn / nice_pattern.exs
Created July 14, 2020 04:27 — forked from costaraphael/nice_pattern.exs
Builder pattern in Elixir (don't do this!!!)
defmodule Person do
defstruct [:name, :age]
def with do
{Person.Builder, %{}}
end
defmodule Builder do
def unquote(:'$handle_undefined_function')(:and_with, [{__MODULE__, _acc} = tuple]) do
tuple
@gvaughn
gvaughn / ecto_postgresql_migration.ex
Created February 2, 2019 01:41 — forked from ibarchenkov/ecto_postgresql_migration.ex
Ecto migration helpers for PostgreSQL.
defmodule MyApp.Migration do
@moduledoc """
Additional helpers for PostgreSQL.
"""
import Ecto.Migration, only: [execute: 2]
defmacro __using__(_) do
quote do
use Ecto.Migration
@gvaughn
gvaughn / application.ex
Created November 23, 2018 17:18 — forked from cblavier/application.ex
Cowboy 2.5 proxy, used to bind a single port (on Heroku) for umbrella Phoenix applications. It supports HTTPS and websockets properly.
defmodule MasterProxy.Application do
alias MyApp.Endpoint, as: MyAppEndpoint
alias MyApp.UserSocket, as: MyAppUserSocket
alias MyOtherApp.Endpoint, as: MyOtherAppEndpoint
alias MyOtherApp.UserSocket, as: MyOtherAppUserSocket
alias Phoenix.LiveReloader.Socket, as: LiveReloadSocket
alias Plug.Cowboy
@gvaughn
gvaughn / discussion.md
Created June 28, 2018 16:32 — forked from jadeallenx/discussion.md
When does terminate/2 get called in a gen_server?

When does terminate/2 get called in a gen_server?

This is what the [official documentation][1] says about the terminate/2 callback for a gen_server:

This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.

Reason is a term denoting the stop reason and State is the internal state of the gen_server.

Reason depends on why the gen_server is terminating. If it is because another callback function has returned a stop tuple {stop,..}, Reason will have the value specified in that tuple. If it is due to a failure, Reason is the error reason.

@gvaughn
gvaughn / ecto_postgres_fulltext_search_querying_example.ex
Created October 4, 2017 22:34 — forked from pmarreck/ecto_postgres_fulltext_search_querying_example.ex
How to set up postgres fulltext search triggers, index, and tsvector column on Elixir/Phoenix, with Ecto querying, including ranking and sorting by rank
defmodule YourAppName.Search do
# ...
@doc """
Queries listings.
"""
def query_listings(query, current_user) do
default_scope = from l in Listing, where: l.draft == false or l.user_id == ^current_user.id, order_by: [desc: l.updated_at], limit: 50
id = _try_integer(query)
@gvaughn
gvaughn / slackpost.sh
Created May 11, 2016 21:43 — forked from mattak/slackpost.sh
post message to slack
#!bash -e
TOKEN= # slack token is generate from: https://api.slack.com/web
CHANNEL= # name of channels or group
MESSAGE= # message
NICK= # bot name
IS_PRIVATE= # 1 or 0
if [ $IS_PRIVATE -eq 1 ]; then
API_TYPE=groups
@gvaughn
gvaughn / golf.exs
Created November 24, 2015 18:27 — forked from henrik/caddy.exs
A convenient script for Elixir golfing.
# A convenient script for Elixir golfing.
# Provide your code and some test cases. Then run this file, e.g. "elixir golf.exs".
# Outputs your character length and test results.
# Text input, text output and return values are all handled.
#
# By Henrik Nyh (http://henrik.nyh.se) under the MIT license.
ExUnit.start
defmodule Golf.Example do