Skip to content

Instantly share code, notes, and snippets.

View ihumanable's full-sized avatar

Matt Nowack ihumanable

  • San Francisco, CA
View GitHub Profile
@ihumanable
ihumanable / Excel.php
Last active February 6, 2024 06:24
Simple Excel Writer in PHP
<?php
/**
* Simple excel writer class with no external dependencies, drop it in and have fun
* @author Matt Nowack
* @link https://gist.github.com/ihumanable/929039/edit
* @license Unlicensed
* @version 1.0
*/
class Excel {
@ihumanable
ihumanable / discord_test.exs
Last active December 3, 2021 21:48
Test Skeleton
defmodule Discord.Test do
use ExUnit.Case
describe "send_message/1" do
test "errors on invalid messages" do
# TODO
end
test "errors when message can't be durably stored" do
# TODO
@ihumanable
ihumanable / discord.ex
Last active December 3, 2021 21:45
If only Discord were this easy
defmodule Discord do
def send_message(%Message{} = message) do
with :ok <- validate_message(message),
:ok <- save_message(message) do
broadcast_message(message)
end
end
defp validate_message(message) do
with :ok <- validate_author_is_member(message),
@ihumanable
ihumanable / call_graph.ex
Created November 23, 2021 00:38
Patch Call Graph
assert {:bad, :error} == Discord.send_message(%Message{})
# We start at the Facade
Discord.send_message(%Message{}) {
# The Facade calls the Delegate
Delegate.send_message(message) {
# The Delegate checks if the Server has a patch for send_message/1
Server.delegate(Discord, :send_message, [message]) {
# The Server doesn't, so it calls the Original
Original.send_message(message) {
@ihumanable
ihumanable / test_output_patch.ex
Created November 23, 2021 00:38
Test Output (Patch)
~/src/discord > mix test
...
3 tests, 0 failures
@ihumanable
ihumanable / discord_test.exs
Created November 23, 2021 00:37
Invalid Message Test Case with Patch
defmodule Discord.Test do
use ExUnit.Case
use Patch
describe "send_message/1" do
test "errors on invalid messages" do
patch(Discord, :validate_message, {:error, :bad})
assert {:error, :bad} == Discord.send_message(%Message{})
end
end
@ihumanable
ihumanable / discord_original.ex
Created November 23, 2021 00:36
Patch Original for Discord
defmodule Patch.Mock.Original.For.Discord do
alias Patch.Mock.Delegate.For.Discord, as: Delegate
def send_message(%Message{} = message) do
with :ok <- Delegate.validate_message(message),
:ok <- Delegate.save_message(message) do
Delegate.broadcast_message(message)
end
end
@ihumanable
ihumanable / server.ex
Created November 23, 2021 00:36
Patch.Mock.Server delegate/3 simplified
def delegate(module, name, arguments) do
server = Naming.server(module)
case GenServer.call(server, {:delegate, name, arguments}) do
{:ok, reply} ->
reply
:error ->
original = Naming.original(module)
apply(original, name, arguments)
@ihumanable
ihumanable / discord_delegate.ex
Created November 23, 2021 00:35
Patch Delegate for Discord
defmodule Patch.Mock.Delegate.For.Discord do
alias Patch.Mock.Server
def send_message(%Message{} = message) do
Server.delegate(Discord, :send_message, [message])
end
def validate_message(message) do
Server.delegate(Discord, :validate_message, [message])
end
@ihumanable
ihumanable / discord_facade.ex
Created November 23, 2021 00:34
Patch Facade for Discord
defmodule Discord do
alias Patch.Mock.Delegate.For.Discord, as: Delegate
def send_message(%Message{} = message) do
Delegate.send_message(message)
end
end